Java Selenium: Get get multiple WebElements of the same class - java

This is the Button I want to press:
<button class="ProfileTweet-actionButton js-actionButton js-actionRetweet" data-modal="ProfileTweet-retweet" type="button">
The same kind of Button exists multiple time on the site.
I tried pressing it by doing this:
By byaXpath = By.xpath("//div[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]");
WebElement Element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byaXpath));
Element.click();
But after 10 secons I just get this error:
org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for presence of element
Im kinda lost now. I tried diffrent solution to similar cases but nothing seems to work.

You have to put button instead of div at the start of your xpath.
"//button[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]"

Related

Is there a better way to programatically select a value from select2 dropdown using selenium WebDriver?

I am using Selenium WebDriver to automate something. It requires filling a form that involves selecting a value from a select2 dropdown. This is the code snippet that I am using-
final By SELECT_DIV = By.id("s2");
click(SELECT_DIV);
final By INPUT = By.cssSelector(".select2-drop-active .select2-input");
waitForVisibilityOfElement(INPUT);
enterCharSequence(INPUT, "someData");
waitForJSandJQueryToLoad(30);//30 seconds
final By LIST_ITEM = By.cssSelector(".select2-drop-active ul.select2-results li.select2-result-selectable");
click(LIST_ITEM);
FYI, there are no unique ids assigned to some of these elements and hence I used css selectors for locating them.
This code works but it sometimes throws a StaleElementReferenceException. This is the error:
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Selenium version : 2.53
So, I want to know if there is any way I could avoid this. I read a few posts about it but they were not of much help.
Let me know if you need more information. Any help would be appreciated.
'StaleElementReferenceException' means that the element has changed. It is in another div, another span or the its properties changed. Selenium may found it but it changed the very second you tried to click on it.
You need to search for the same element again and wait for it to be clickAble or visible. For example:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement button =
wait.until(ExpectedConditions.
elementToBeClickable(By.id("btnLogin")));

Unable to click on webelement button

Unable to click on webelement button
I tried to click on button by mouse movement but no success
my outer html is as below :
<button class="btn btn-alt btn-small" type="button" ng-click="ecdapp.uploadBlueprintModalPopup();">
Create
</button>
button xpath is:
//*[#id="page-content"]/div[3]/button
Not seeing the full page source it's hard to tell where your XPath expression is good or not, you can try locating the button using its text instead
//button[normalize-space(text())='Create']
the normalize-space() function is used to discard heading/trailing whitespaces
It might also be the case the button is not immediately available, I would recommend considering using Explicit Wait approach via WebDriverWait class
WebElement myButton = new WebDriverWait(driver, 10)
.until(ExpectedConditions
.elementToBeClickable(By.xpath("//button[normalize-space(text())='Create']")));
myButton.click();
the above code will try to locate the aforementioned button for 10 seconds and click it as soon as it will be present/visible/clickable. Otherwise it will fail with NoSuchElementException
May be the Xpath is wrong. Try the below xpath:
//button[contains(text(),'Create')]
As you can see on the screenshot this Xpath 100% works, if you still won't be able to click on that button, then problem is not with xpath. Let me know if its still fails.
By.xpath("//button[#class = 'btn btn-alt btn-small' and #type = 'button']")
Based on your comment:
I tried this code , but unable to click . element click intercepted: Element ... is not clickable at point (293, 97). Other element would receive the click: ... (Session info: chrome=74.0.3729.169)
I pretty sure I know whats your problem, before u click on this element, something going on the page:
It says - Other element would receive the click, means there is other element above(overlapping) your button(pop up window, page is greyed out(disabled while loading, Some JS running)), so when Selenium trying to click on your button its actually clicking on that blocking element.
Try to click after Thread.Sleep(); wait 5-10 sec.
If this is the case then u need to add condition before find your button to check that element that prevent from clicking on button is disappeared then u click on it.
Try JavaScript executors as below,
WebElement element = driver.findElement(By.xpath("<xpath of button>"));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);

Selenium: Clicked Element changes depending on number of instance launched

So I'm just trying to get the value of a input textbox after clicking a radiobutton related to it. I have 3 radiobuttons, let's call them "radio1" "radio2" and "radio3"
By default, "radio3" is checked, my goal is to click "radio1" and then get the value of the textbox. Here is the code I'm running:
WebElement radioMonthly = driver.findElement(By.xpath("//*
[#id=\"dateFromTo_date_format_2\"]"));
Actions actions = new Actions(driver);
actions.moveToElement(radioMonthly).click().perform();
At first attempt, "radio1" is clicked and the value stored properly, but if I launch the test again, it's "radio2" the clicked one. Note that they don't share id.
I already fixed the issue but I would like to know what's going on here
Thanks
Here is how I fixed it, following this thread on SO
Debugging "Element is not clickable at point" error
And the code:
WebElement radioMonthly =
driver.findElement(By.xpath("//[#id=\"dateFromTo_date_format_2\"]"));
JavascriptExecutor clickradioMonthly = (JavascriptExecutor)driver;
clickradioMonthly.executeScript("arguments[0].click()", radioMonthly);
But still will be great if someone is able to understand and explain whats going on the question
Thanks

Unable to locate button and href in Selenium

I am trying to locate some elements in Selenium and I have problem.
There are no normal elements like ID etc.
Issue no 1 --> should click to Continue button
<a _ngcontent-c3="" routerlink="cms" href="/cms">Continue</a>
Issue no 2 --> should click to Add button
<button _ngcontent-c11="" aria-label="Add user" class="action-btn add" type="button">
Issue 2 html screenshot
I tried using fidning elements by PartialLinkText, I tried to make my own CSS selector/xPath, but test case always fails...
One of unsuccessful solutions for Issue 1:
WebElement continueButton = driver.findElement(By.partialLinkText("Continue"));
continueButton.click();
Any suggestions?
Thanks.
You can find button by class and i hope using this xpath your problem will be
Here your possible xpath is //a[#routerlink='cms'][text()='Continue'] and //button[#aria-label='Add user']/i']
WebElement Continue = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#routerlink='cms'][text()='Continue']")));
Continue.click();
WebElement adduser = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#aria-label='Add user']/i")));
adduser.click();

Selenium: select an item in combobox

I have been trying to perform a selenium task on it:
In this page, there is a button which i have to click on it and then wait for 10 seconds. I did it like this:
Naviagation to page:
base.driver.navigate().to("http://suvian.in/selenium/1.7button.html");
Click on button:
//base.driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div/h3[2]/a"));
base.driver.findElement(By.linkText("Click Me"));
This step fails
Wait for 10 seconds:
TimeUnit.SECONDS.sleep(waitTime);
Questions:
1-it fails on clicking on the button. Although, i asked to find the link both with xpath, and text it cannot find it?
2-Is my solution correct for make a delay on webdriver's activity?
Try this code using xpath locator.
Note: Instead of using absolute xpath, use relative xpath.
new Select(driver.findElement(By.xpath("//select[#name='gender']"))).selectByVisibleText("Male");
OR
new Select(driver.findElement(By.xpath("//select[#name='gender']"))).selectByValue("1");
In the dropdown there are 3 options. The first one is Select, Male is the second. Try
select.selectByIndex(1);

Categories