Selenium: Clicked Element changes depending on number of instance launched - java

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

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);

How to click button using selenium webdriver java, whose xpath is not constant

I am new to selenium webdriver in Java. I am using webdriver with Firefox Quantum (59.0, x64). The problem I am facing is that the website i am writing code for testing dose not have constant xpath. On every visit the button id changes. Even the classname(launchbutton) is same for all 4 Launch Buttons. So while writing code it initially worked & opened the first link, but for second link it again opened the first link as the classname is same for all 4 buttons. Please help me out to code to open other links too by pressing other launch button.
i used this(below) code but it worked partially. Please help me out as i want to launch using button of other subjects too. (Please refer screenshot)checkout screenshot1 here
checkout screenshot2 here
Also i am not able to switch between tabs. pls help.
thankyou
driver.findElement(By.className("launchbutton")).click();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
driver.get("http://google.com");
//Grab all elements which have the className "launchbutton"
List<WebElement> elements = driver.findElements(By.className("launchbutton"));
//Iterate your list of matching elements and look at the innerText for the button you want
//When found, click it and break the for loop
foreach(WebElement ele in elements)
{
if(ele.getAttribute("innerText") == "The button text you want")
{
ele.Click();
break;
}
}
You can click the Launch button as below and please change the code based on the mentioned comments
//Specify the Expected Name in the below String
String subjectName="";
//Find the Table Element using any one of the Unique Locator
WebElement table=driver.findElement(By.id(""));
List<WebElement> rowElementList=table.findElements(By.xpath(".//tr"));
//I have assumed Launch button column in 5th.So, Specified as td[4].
//Please cross check the Launch button column and change the index accordingly
for(WebElement element:rowElementList){
if(element.findElement(By.xpath("./td")).getText().equalsIgnoreCase(subjectName)){
element.findElement(By.xpath("./td[4]/a")).click();
}
}

Can't Select Checkbox, Selenium WebDriver

I am unable to select a checkbox with Selenium WebDriver in Java.
I tried by Xpath but no result.
WebDriver can't click on element.
I tried with Selenium IDE - recorder, no results.
Here it is - html code for checkbox
I try:
1.
driver.findElement(By.xpath(".//form[#id='placeOrderForm1']/div[#class='terms right']/label")).click();
2.
driver.findElement(By.id("Terms1")).click();
3.
driver.findElement(By.cssSelector("label")).click();
4.
driver.findElement(By.xpath("//div[3]/form/div/input")).click();
Nothing works.
Please help.
Try using JavascriptExecuter Hope this will help
WebElement element = driver.findElement(By.id("Terms1"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", element );
Your code seems correct. Specially this one -
driver.findElement(By.id("Terms1")).click();
It might be possible the element you are clicking is not visible in the page scroll. Try to move to the element first and then click.
Try with this -
WebElement elem = driver.findElement(By.id("Term1"));
Actions action = new Actions(driver).
action.moveToElement(elem).click().build().perform();
Hope this help.
Here is the Answer of your Question:
As you mentioned unable to select a checkbox, actually we don't select the checkbox, we checkmark the checkbox. The checkbox you depicted have an id as Terms1 and name astermsCheck`. So you use either of the locators to checkmark the checkbox as follows:
driver.findElement(By.id("Terms1")).click();
OR
element = driver.findElement(By.name("termsCheck")).click();
Let me know if this Answers your Question.
You can find the element by a unique identifier. In this case, we can use name or id. The better choice is to go with id.
WebElement element = driver.findElement(By.name("termsCheck"));
element.click();
or you can use this one also
driver.findElement(By.id("Terms1")).click();

Unable to click on pop up screen

After entering values to few fields i click on submit button which produces a pop up screen where i should click go button. I tried below code, it worked once but not working now. Please help
WebDriverWait wait = new WebDriverWait(driver, 6);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='lets_go']")));
driver.findElement(By.xpath(".//*[#id='lets_go']")).click();
How to fix ?
If it's an alert the use:
driver.switchto().alert().accept().
If pop up is a window, then first switch to that window using WindowHandler, then click on element
ExpectedConditions.visibilityOfElementLocated is used for checking that an element is present on the DOM of a page and visible, if this condition true within given time limit, it returns WebElement otherwise throws TimeOutException, So There is no need to find element again, omit last line and try as below :
WebDriverWait wait = new WebDriverWait(driver, 6);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lets_go")));
el.click();
I would suggest pasting the snippet of code for that "popup" to get your code working along with stack trace of error you are getting.
Also, you can go through http://www.softwaretestinghelp.com/handle-alerts-popups-selenium-webdriver-selenium-tutorial-16/. It gives you clear idea when it comes to handling popup.

Categories