span class locator is not visible/clickable - selenium - java

Following is the HTML for span:
<td class="header-logout-btn">
<a href="logout.htm" class="btn switch-btn">
<i class="fa fa-times"></i><span class="hidden-xs">Home</span>
</a>
</td>
Tried with:
driver.findElement(By.linkText("HOME"));
driver.findElement(By.xpath("//div[#class="header-logout-btn"]/span[#class="hidden-xs"]));
driver.findElement(By.xpath("//span"));
driver.findElement(By.className("hidden-xs"));

Try following solution :
Xpath= //*[contains(text(),'Home')]
Hope it will help you.

Normally the logout button will be submenu or it will be visible only if you click or mouse over on main menu usually the main menu is profile icon. I assume this logout button visible only after clicking on main/profile button. This may be the reason the button is hidden or not clickable. The solutions are given below.
1. First click or mouse over on main/profile menu then click on logout button.
2. Still you want to click on hidden button. You may be try with JavaScript executor as given below.
Webelement eleLogout=driver.find element(By.class name("switch-btn"));
JavaScriptExecutor js=(JavaScriptExecutor)driver;
js.executeScript("arguments [0].click()",eleLogout);`

Try clicking on the link.
Use selectors like:
xpath:
//a[contains(#href, 'logout')] or
//*[#class='header-logout-btn']/a[contains(#href, 'logout')]
css:
a[href*=logout] or
.header-logout-btn a[href*=logout]

Related

How to click on an element inside span tag? - Selenium

Hi I am trying to click on a specific button with text "€11" on it in selenium webdriver in Java.
The button is located within this format and there is also another button on the page with exactly the same code but different price:
<button type="button" class="bui-button bui-spacer--medium bui-button--primary bui-button--wide">
<span class="bui-button__text">€11</span>
</button>
How would I do this. Any help is appreciated :)
Full CSS
I know some ways that could help
javascript:
document.querySelector("span.bui-button__text").click()
jquery:
$("body > button > span").click()
Selenium (Java):
driver.findelement(By.cssSelector("button[class='bui-button bui-spacer--medium bui-button--primary bui-button--wide']").click()
I hope that they help you !
A few options you could use:
driver.findElement(By.xpath("//span[contains(text(),'€11')]")).click();
Click first button using this:
driver.findElement(By.xpath("(//span[#class='bui-button__text'])[1]")).click();
Click second button using this:
driver.findElement(By.xpath("(//span[#class='bui-button__text'])[2]")).click();
Note: use wait's if you have any delays in loading amount

How can I select this button?

I am using Selenium to automate the checkout process of a website and can not figure out how to select this certain element properly. So far I have tried all of the following with no luck:
//no such element
driver.findElement(By.id("Something here")).click();
//no such element
driver.findElement(By.xpath("//*[#id="add-remove-buttons"]/input")).click();
//no such element
driver.findElement(By.name("commit")).click();
It seems like the closest I've gotten is with:
driver.findElement(By.className("button")).click();
Using this gives me an error saying the button is not visible. So it looks like I'm heading in the right direction with this, but I'm not sure where to go from here. Here is the HTML source of the button:
<input type="submit" name="commit" value="add to cart" class="button">
not sure if this plays a part in it but when this button is clicked it changes into a new button. The same location, but different color, text, and functionality.
If you wanted to take a look for yourself here is the website:
http://www.supremenewyork.com/shop/accessories/yf89tm27c/e8c56njah?alt=0
and the button I am trying to click is the add to cart button.
Try the following code:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.name("commit")));
button.click();
Hope it helps you!

How to use Button class In Selenium Webdriver by the help of Java

Here is my code:
<div class="meal_buton_cont">
<button class="pink_button" data-target="#myModal" data-toggle="modal">Add Selected to Shopping List</button>
<button id="add_meal" class="gray_button">+ Add Meal</button>
</div>
I am using this:
driver.findElement(By.className("pink_button")).click();
But no popup open but some time open but not item take, it show blank.
How to create script for clicking Pink_button then my popup will be open?
Please help me
Few things to consider..Wait before the element appears using explicit waits like WebDriverWait API. Once the element is clickable using ExpectedConditions.elementToBeclickable(WebElement) then click on it and again wait till the pop-up appears. if its alert then use ExpectedConditions.alsertIsPresent() with WebDriverWait

Selenium Webdriver - unable to click on button

HTML code:
<div class="buttonBG">
<input type="button" onclick="window.location.href='?mod=eA&act=00001';" class="buttonGreen" value="TK">
<input type="button" onclick="ttoggle('carianDiv');" class="buttonGreen" value="CK">
</div>
Below is my java code, when I try with below code. Can I know whats wrong is in my selenium webdriver code:
driver.findElement(By.xpath("//input[#class='buttonGreen' and contains(#onclick, 'window.location.href='?mod=eA&act=00001')]")).click();
Try to search by value
driver.findElement(By.cssSelector("[value='TK']")).click();
And for what's wrong, you are searching for ?mod=eA&act=00001 when in the html its
?mod=eA&act=00001
Edit
Another solution is to insert the buttons to list and click by index:
List<WebElement> buttons = driver.findElements(By.className("buttonGreen"));
buttons.get(0).click();
You can also try using explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[value='TK']")).click();
This will wait up to 10 seconds for the button to be visible before clicking on it. You can change the selector or time span.
Try using XPath instead of CSS
driver.find_element(By.XPATH, '//input[#onclick=\'window.location.href=\'?mod=eA&act=00001\';\']').click()
Edit
Here is the code to switch to iFrame,
driver.switchTo().frame("frame_name");
NOTE: After completing the operation inside the iframe, you have to again return back to the main window using the following command.
driver.switchTo().defaultContent();

InternetExplorer Webdriver with nativeEvents

I have a menu hidden in a button, when the button is clicked, then the menu is shown, the structure of hidden menu is follow:
<button id="buttonID"></button>
<ul class="ulClass">
<li>
<li>
<li>
<li>
</ul>
I want to click on second item, so I did:
webDriver.findElement(By.xpath("//ul[#class='ulClass']/li[2]")).click();
It works fine with FF and Chrome, but doesn't work with IE, the reason because I gave the nativeEvents to false to IE:
capabilities.setCapability("nativeEvents", false);
This capabilities is set for the whole test with IE, without it, the whole test won't work, and now I just need to click on this item but can't because of this capabilities.
Is there any workaround to click on this item but I still skip this capability, because apparently we can't toggle capabilities in run time. Thanks.
I'd try JavaScript instead native clicks
var element=webDriver.findElement(By.xpath("//ul[#class='ulClass']/li[2]"));
Driver.ExecuteJavaScript("arguments[0].click();",element);
Thanks for all suggestions, I solved this with JavascriptExecutor:
JavascriptExecutor js = (JavascriptExecutor)webDriver;
js.executeScript("arguments[0].click();", element);

Categories

Resources