I have been trying a simple program that navigates and fetches data from the new page, comes back in history and open other page and fetch data and so on until all the links have been visited and data is fetched.
After getting results on the below site, i am trying to loop through all the links i get in the first column and open those links one by one and extract text from each of these page. But the below program only visits first link and gives StaleElementReferenceException, I have tried using Actions but it didn't work and I am not aware about JavascriptExecutor. I also tried solutions posted on other SO questions, one of which was mine over here. I would like to have the mistake corrected in the below code and a working code.
public class Selenium {
private final static String CHROME_DRIVER = "C:\\Selenium\\chromedriver\\chromedriver.exe";
private static WebDriver driver = null;
private static WebDriverWait wait = null;
private void setConnection() {
try {
System.setProperty("webdriver.chrome.driver", CHROME_DRIVER);
driver = ChromeDriver.class.newInstance();
wait = new WebDriverWait(driver, 5);
driver.get("https://sanctionssearch.ofac.treas.gov");
this.search();
} catch (Exception e) {
e.printStackTrace();
}
}
private void search() {
try {
driver.findElement(By.id("ctl00_MainContent_txtLastName")).sendKeys("Dawood");
driver.findElement(By.id("ctl00_MainContent_btnSearch")).click();
this.extractText();
} catch (Exception e) {
e.printStackTrace();
}
}
private void extractText() {
try {
List<WebElement> rows = driver.findElements(By.xpath("//*[#id='gvSearchResults']/tbody/tr"));
List<WebElement> links = null;
for (int i = 1; i <= rows.size(); i++) {
links = driver.findElements(By.xpath("//*[#id='gvSearchResults']/tbody/tr/td[1]/a"));
for (int j = 0; j < links.size(); j++) {
System.out.println(links.get(j).getText() + ", ");
links.get(j).click();
System.out.println("Afte click");
driver.findElement(By.id("ctl00_MainContent_btnBack")).click();
this.search();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] ar) {
Selenium object = new Selenium();
object.setConnection();
}
}
Generally we will be getting the Stale Exception if the element attributes or something is changed after initiating the webelement. For example, in some cases if user tries to click on the same element on the same page but after page refresh, gets staleelement exception.
To overcome this, we can create the fresh webelement in case if the page is changed or refreshed. Below code can give you some idea.
Example:
webElement element = driver.findElement(by.xpath("//*[#id='StackOverflow']"));
element.click();
//page is refreshed
element.click();//This will obviously throw stale exception
To overcome this, we can store the xpath in some string and use it create a fresh webelement as we go.
String xpath = "//*[#id='StackOverflow']";
driver.findElement(by.xpath(xpath)).click();
//page has been refreshed. Now create a new element and work on it
driver.fineElement(by.xpath(xpath)).click(); //This works
In this case, we are collecting a group of webelements and iterating to get the text. But it seems there is some changes in the webelement after collecting the webelements and gettext throws staleness. We can use a loop and create the element on the go and get text.
for(int i = 0; i<5; i++)
{
String value = driver.findElement(by.xpath("//.....["+i+"]")).getText);
System.out.println(value);
}
Hope this helps you. Thanks.
The reason you get StaleElementReference Exception, is normally because you stored element(s) into some variable, however after that you did some action and page has changed (due to some ajax response) and so your stored element has become stale.
The best solution is not to store element in any variable in such case.
This should work.
links = driver.findElements(By.xpath("//*[#id='gvSearchResults']/tbody/tr/td[1]/a"));
for (int j = 0; j < links.size(); j++) {
System.out.println(links.get(j).getText() + ", ");
driver.findElements(By.xpath("//*[#id='gvSearchResults']/tbody/tr/td[1]/a")).get(j).click();
System.out.println("Afte click");
driver.findElement(By.id("ctl00_MainContent_btnBack")).click();
this.search();
}
Please check this code
private void extractText() {
try {
List<WebElement> rows = driver.findElements(By.xpath("//*[#id='gvSearchResults']/tbody/tr"));
List<WebElement> links = null;
System.out.println(rows.size());
for (int i = 0; i < rows.size(); i++) {
links = driver.findElements(By.xpath("//*[#id='gvSearchResults']/tbody/tr/td[1]/a"));
WebElement ele= links.get(0);
System.out.println(ele.getText() + ", ");
ele.click();
System.out.println("After click");
driver.findElement(By.id("ctl00_MainContent_btnBack")).click();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Related
I want to know how to nest dropdowns using selenium webdriver using java,i.e., I have 2 dropdowns and can these dropdowns be nested one after the other?
After looping 2 times for a dropdown it is showing stale element reference error
I have written the following code:
Select drpdwns6 = new Select(driver.findElement(By.xpath("//*[#id=\"MainContent_ddlBillable\"]")));
List <WebElement> sels6 = drpdwns6.getOptions();
sels6.size();
for(int s6=0;s6<sels6.size();s6++) {
drpdwns6.selectByIndex(s6);
System.out.println("selected value"+s6);
Select drpdwns7 = new Select(driver.findElement(By.xpath("//*[#id=\"MainContent_ddlofflinestatus\"]")));
List <WebElement> sels7 = drpdwns7.getOptions();
sels7.size();
for(int s7=0;s7<sels7.size();s7++) {
drpdwns7.selectByIndex(s7);
System.out.println("selected value"+s7);
}
}
My guess is selecting the option from the dropdown refreshes the DOM, so the exception is thrown. You need to relocate the dropdown in each itreation
Select drpdwns6 = new Select(driver.findElement(By.id("MainContent_ddlBillable")));
int drpdwns6Size = drpdwns6.getOptions().size();
for(int s6 = 0 ; s6 < drpdwns6Size ; s6++) {
drpdwns6 = new Select(driver.findElement(By.id("MainContent_ddlBillable")));
drpdwns6.selectByIndex(s6);
System.out.println("selected value"+s6);
Select drpdwns7 = new Select(driver.findElement(By.id("MainContent_ddlofflinestatus")));
int drpdwns7Size = drpdwns7.getOptions().size();
for(int s7 = 0 ; drpdwns7Size ; s7++) {
drpdwns7 = new Select(driver.findElement(By.id("MainContent_ddlofflinestatus")));
drpdwns7.selectByIndex(s7);
System.out.println("selected value"+s7);
}
}
As a side note, if you have an id use By.id it instead of By.xpath
You get the Stale element exception whenever the element present in the DOM is deleted or removed or is not available.
The above answer (ie) relocating the element once the DOM is refreshed or you could use Webdriver wait, If an element is not attached to DOM then you could try using ‘try-catch block’ within ‘for loop’ like below
driver.manage().timeouts().implicitlywait(30,TimeUnit.SECONDS);
try{
Select drpdwns6 = new
Select(driver.findElementByXpath("//[#id=\"MainContent_ddlBillable\"]")));
List <WebElement> sels6AllOptions = drpdwns6.getOptions();
int count1=sels6AllOptions.size();
for(int s6=0;s6<count1;s6++)
{
drpdwns6.selectByIndex(s6);
}
}
catch(StaleElementException e1){
System.out.println("selected value"+s6);
}
try{
Select drpdwns7 = new Select(driver.findElement(By.xpath("//*[#id=\"MainContent_ddlofflinestatus\"]")));
List <WebElement> sels7AllOptions = drpdwns7.getOptions();
int count2=sels7AllOptions.size();
for(int s7=0;s7<count2;s7++) {
drpdwns7.selectByIndex(s7);
catch(StaleElementException e2){
System.out.println("selected value"+s7);
}
}
I try to retrieve text via foreach loop,as according to page wise. Flow is : It prints text of single row and as soon as it completes, it goes to second page and start again to retrieve text. Problem is, it retrieves data of first page multiple times like sometimes 2 or 3 or 4 times, How to control it for single time execution ?
if (driver.findElement(By.xpath("//button[#ng-click='currentPage=currentPage+1']")).isEnabled()) {
int ilength = driver.findElements(By.xpath("//input[#ng-attr-id='{{item.attr}}']")).size();
Outer: for (int i1 = ilength; i1 > 0;) {
List<WebElement> findData = driver.findElements(By.xpath("//input[#ng-attr-id='{{item.attr}}']"));
for (WebElement webElement : findData) {
String printGroupName = webElement.getAttribute("value").toString();
System.out.println(printGroupName);
ilength--;
}
if (driver.findElement(By.xpath("//button[#ng-click='currentPage=currentPage+1']")).isEnabled()) {
action.moveToElement(driver.findElement(By.xpath("//button[#ng-click='currentPage=currentPage+1']"))).click().perform();
page.pagecallingUtility();
ilength = driver.findElements(By.xpath("//input[#ng-attr-id='{{item.attr}}']")).size();
} else {
break Outer;
}
}
} else {
List<WebElement> findAllGroupName = driver.findElements(By.xpath("//input[#ng-attr-id='{{item.attr}}']"));
for (WebElement webElement : findAllGroupName) {
String printGroupName = webElement.getAttribute("value").toString();
System.out.println(printGroupName);
}
}
Console Data, on which it retrieve information
Your loop can be simplified as below.
boolean newPageOpened = true;
while (newPageOpened) {
List<WebElement> findData = driver.findElements(By.xpath("//input[#ng-attr-id='{{item.attr}}']"));
for (WebElement webElement : findData) {
if (webElement.isDisplayed()) {
String printGroupName = webElement.getAttribute("value").toString();
System.out.println(printGroupName);
}
}
WebElement nextButton = driver.findElement(By.xpath("//button[#ng-click='currentPage=currentPage+1']"));
if (nextButton.isEnabled()) {
action.moveToElement(nextButton).click().perform();
page.pagecallingUtility();
} else {
newPageOpened = false;
}
}
As for the contents of the fist page printing again and again, I suspect when you open the second page the contents of the first page are simply hidden in the page. So when you use driver.findElements(By.xpath("//input[#ng-attr-id='{{item.attr}}']")) the hidden first page elements are also found. The simple solution is to check if the element is displayed before printing it.
I am trying to enter the text and ensure it gets saved. In my project, there is no save button, as soon as it entered it gets saved. Only few fields that cracks me when entering the text.
Note: I'm using the latest version of Chrome and passing the numeric values as text.
I referred to below pages and updated my code accordingly, then also this issue persists:
Selenium Webdriver: Entering text into text field
Selenium send_keys doesn't work if input type="number"
Below is the code that I used,
public void EnterValuesByIndex(String locator, String locatorValue, String text, int indexvalue) throws InterruptedException
{
WebElement element = null;
if (locator.equalsIgnoreCase("cssSelector")) {
element = (WebElement)driver.findElements(By.cssSelector(locatorValue)).get(indexvalue - 1);
} else if (locator.equalsIgnoreCase("xpath")) {
element = (WebElement)driver.findElements(By.xpath(locatorValue)).get(indexvalue - 1);
} else if (locator.equalsIgnoreCase("id")) {
element = (WebElement)driver.findElements(By.id(locatorValue)).get(indexvalue - 1);
}
//element.clear();
element.sendKeys(text);
Thread.sleep(2000);
element.sendKeys(Keys.ENTER);
System.out.println("Enter key is pressed");
}
Below code also used to enter character by character, same issue persists,
public void EnterTextbyChar(String locator, String locatorValue, String text, int indexvalue) throws InterruptedException
{
String value = text;
WebElement element = null;
if (locator.equalsIgnoreCase("cssSelector")) {
element = (WebElement)driver.findElements(By.cssSelector(locatorValue)).get(indexvalue - 1);
} else if (locator.equalsIgnoreCase("xpath")) {
element = (WebElement)driver.findElements(By.xpath(locatorValue)).get(indexvalue - 1);
} else if (locator.equalsIgnoreCase("id")){
element = (WebElement)driver.findElements(By.id(locatorValue)).get(indexvalue - 1); }
element.clear();
for (int i = 0; i < value.length(); i++)
{
char c = value.charAt(i);
String s = new StringBuilder().append(c).toString();
element.sendKeys(s);
element.sendKeys(Keys.RETURN);
element.click();
Thread.sleep(2000);
System.out.println("Return key is pressed in EnterTextByChar method");
System.out.println(c);
}
}
Finally a solution found. After entering the text, sending the upwards key that worked perfectly.
element.sendKeys(text);
element.sendKeys(Keys.UP);
If other options don't work, try
element.sendKeys(text);
element.sendKeys(Keys.RETURN);
Here is a code fragment:
System.setProperty(Constants.WEBDRIVER_CHROME_DRIVER_PROP, Constants.WEBDRIVER_CHROME_DRIVER_PATH);
m_chromeWebdriver = new ChromeDriver();
m_chromeWebdriver.get("mysite.org");
WebElement arrowElement = m_chromeWebdriver.findElement(By.cssSelector(_ARROW_NEXT_DAY));
arrowElement.click();
WebElement elmMainTable = m_chromeWebdriver.findElement(By.className("table-main"));
List<WebElement> allRows = elmMainTable.findElements(By.tagName("tr"));
for (WebElement row : allRows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
System.out.println(cell.getText());
}
}
m_chromeWebdriver.quit();
At the last line I get an
"stale element reference: element is not attached to the page
document"
exception.
Why and how can I solve that?
I use Chromdriver 2.2.9.
Well, as this is probably not the perfect solution - at least it worked for me...
I put all the relevant code into a method like this:
private static void handleTable() {
for (int i = 1; i < 5; i++) {
try {
WebElement elmMainTable = m_chromeWebdriver.findElement(By.className("table-main"));
List<WebElement> allRows = elmMainTable.findElements(By.cssSelector(".table-main tr"));
for (WebElement row : allRows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
System.out.print(cell.getText() + "\t");
}
System.out.println();
}
} catch (StaleElementReferenceException e) {
//e.printStackTrace();
handleTable();
}
return;
}
}
And it worked! You can change the value 5 to which ever you'd like of course.
I learned the main reason for Selenium stale exception is element changed in DOM. In case, the stale exception are caused by those web elements you are not handling, you can try below try-catch and continue method in JAVA.
//get webelement list of button on the OU tab
List<WebElement> ouList =
Logon.wDriver.findElements(By.tagName(PagePropertise.tagButton));
// it should be 6
System.out.println("List Size is: "+ouList.size());
for (int i=0; i<ouList.size(); i++ ) {
//get each button element to verify with input Org name
WebElement ouName= ouList.get(i);
String ouLabel = null;
try {
ouLabel = ouName.getText();
} catch (StaleElementReferenceException e) {
//handle in exception catch, just skip invalid element and continue for to handle rest of loop
System.out.println("WebElement "+i+" = text "+ ouLabel);
continue;
}
System.out.println("WebElement "+i+" = text "+ ouLabel);
//webelement processing
...
//for some reason some of webelements in the OUlist were changed for above processing code.
}
I have a table I am trying to test. Inside one of the columns is yes/no radio buttons which when the page is rendered is hidden underneath an image so that there is a check-mark or an X when yes or no is selected.
The attribute for the radio button when yes is selected checked="true".
How do I with Selenium2 get that attribute so that I can compare it or make assertions against it?
I have tried the following, which simply results in a null pointer error.
private void verifyPOEligibleYesNull(String driverName)
{
int min = 1;
int max = 6;
JavascriptExecutor js = (JavascriptExecutor)driver;
StringBuilder stringBuilder = new StringBuilder();
//Navigate to System Facing
driver = browser.getDriver(driverName);
driver.manage().window().maximize();
driver.navigate().to("http://theURL");
//Enter userName
WebElement userName = driver.findElement(By.id("username"));
userName.clear();
userName.sendKeys(user);
//Enter Password
WebElement password = driver.findElement(By.id("pword"));
password.clear();
password.sendKeys(pwd);
//Click Login
WebElement loginBtn = driver.findElement(By.xpath("/html/body/div/div/form/div[3]/div/input"));
loginBtn.click();
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement ivToPoBtn = driver.findElement(By.xpath("/html/body/div/div/div/ul/li/a/img"));
ivToPoBtn.click();
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//Grab a random row and determine whether the PO Eligible is "Yes"
String rowNum = Integer.toString(rand.nextInt(max - min + 1) + min);
WebElement randomRow = driver.findElement(By.xpath("/html/body/div/div/div/div/form/div/div[2]/div/div[2]/div/div/div/div/div/div/div/table/tbody/tr["+rowNum+"]/td/div/div/input"));
String poEligibleChecked = randomRow.getAttribute("checked").toString();
stringBuilder.append("var x = $(\""+poEligibleChecked+"\");");
stringBuilder.append("return x.toString();");
String res = (String) js.executeScript(stringBuilder.toString());
System.out.println(res);
}
I have even tried the following which was suggested from WebDriver FAQ:
WebElement randomRow = driver.findElement(By.xpath("/html/body/div/div/div/div/form/div/div[2]/div/div[2]/div/div/div/div/div/div/div/table/tbody/tr["+rowNum+"]/td/div/div/input"));
String poEligibleChecked = randomRow.getAttribute("checked");
String element = js.executeScript("return arguments[0].getText();", poEligibleChecked).toString();
System.out.println(element);
With similar null pointer exception error.
Your code doesn't really make sense. In order to get the checked attribute of an invisible element, simply use element.getAttribute():
final String scriptGetChecked = "return arguments[0].getAttribute('checked')";
WebElement randomRow = driver.findElement(By.whatever("something"));
String checkedState = (String) js.executeScript(scriptGetChecked, randomRow);