Stale element reference: element is not attached to the page document for Java-Selenium
What I am trying to do is after logging in, driver will click on Property dropdown, select an option and click on submit and repeat the process until the loop is complete.
Below is my code:
package com.genericlibrary;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.util.Highlighter;
public class MobopsSearchJobsFromDropDown {
WebDriver driver;
Highlighter color;
public void getSetup() {
String path = System.getProperty("user.dir");
String driverPath = path + "\Driver\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("http://mobops-test.jcdecauxna.com/");
driver.manage().window().maximize();
}
public void logIntoMobops() {
WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));
userName.sendKeys("test2");
passWord.sendKeys("1234");
loginButton.click();
}
public void selectEachPropertyAndSeachJob() {
WebElement dateRange = driver.findElement(By.xpath("//*[contains(@name,'date_range')]"));
WebElement last7days = driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]"));
WebElement searchJobs = driver.findElement(By.xpath("//*[contains(@name,'layout')]"));
WebElement propertyDropdown = driver.findElement(By.xpath("//*[contains(@id,'property_id')]"));
Select dropdown = new Select(propertyDropdown);
List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
if (propertyDropdown.isDisplayed() && propertyDropdown.isEnabled()) {
try {
propertyDropdown.click();
dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
dateRange.click();
last7days.click();
searchJobs.click();
System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());
} catch (org.openqa.selenium.StaleElementReferenceException ex) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(propertyDropdown));
}
}
}
}
public static void main(String args) {
MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
obj.getSetup();
obj.logIntoMobops();
obj.selectEachPropertyAndSeachJob();
}
}
Right now it driver just select the first option and clicks on submit. Right after the page loads after the search completes I get the following error:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
I have tried to implement the following code in my code, but since I am a novice, I have no idea how to implement the following to fix the issue:
new WebDriverWait(driver, timeout)
.ignoring(StaleElementReferenceException.class)
.until((WebDriver d) -> {
d.findElement(By.xpath("//*[contains(@id,'property_id')]")).click();
return true;
});
Any help to overcome this issue is greatly appreciated. Thanks.
java selenium staleelementreferenceexception
add a comment |
What I am trying to do is after logging in, driver will click on Property dropdown, select an option and click on submit and repeat the process until the loop is complete.
Below is my code:
package com.genericlibrary;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.util.Highlighter;
public class MobopsSearchJobsFromDropDown {
WebDriver driver;
Highlighter color;
public void getSetup() {
String path = System.getProperty("user.dir");
String driverPath = path + "\Driver\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("http://mobops-test.jcdecauxna.com/");
driver.manage().window().maximize();
}
public void logIntoMobops() {
WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));
userName.sendKeys("test2");
passWord.sendKeys("1234");
loginButton.click();
}
public void selectEachPropertyAndSeachJob() {
WebElement dateRange = driver.findElement(By.xpath("//*[contains(@name,'date_range')]"));
WebElement last7days = driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]"));
WebElement searchJobs = driver.findElement(By.xpath("//*[contains(@name,'layout')]"));
WebElement propertyDropdown = driver.findElement(By.xpath("//*[contains(@id,'property_id')]"));
Select dropdown = new Select(propertyDropdown);
List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
if (propertyDropdown.isDisplayed() && propertyDropdown.isEnabled()) {
try {
propertyDropdown.click();
dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
dateRange.click();
last7days.click();
searchJobs.click();
System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());
} catch (org.openqa.selenium.StaleElementReferenceException ex) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(propertyDropdown));
}
}
}
}
public static void main(String args) {
MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
obj.getSetup();
obj.logIntoMobops();
obj.selectEachPropertyAndSeachJob();
}
}
Right now it driver just select the first option and clicks on submit. Right after the page loads after the search completes I get the following error:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
I have tried to implement the following code in my code, but since I am a novice, I have no idea how to implement the following to fix the issue:
new WebDriverWait(driver, timeout)
.ignoring(StaleElementReferenceException.class)
.until((WebDriver d) -> {
d.findElement(By.xpath("//*[contains(@id,'property_id')]")).click();
return true;
});
Any help to overcome this issue is greatly appreciated. Thanks.
java selenium staleelementreferenceexception
add a comment |
What I am trying to do is after logging in, driver will click on Property dropdown, select an option and click on submit and repeat the process until the loop is complete.
Below is my code:
package com.genericlibrary;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.util.Highlighter;
public class MobopsSearchJobsFromDropDown {
WebDriver driver;
Highlighter color;
public void getSetup() {
String path = System.getProperty("user.dir");
String driverPath = path + "\Driver\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("http://mobops-test.jcdecauxna.com/");
driver.manage().window().maximize();
}
public void logIntoMobops() {
WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));
userName.sendKeys("test2");
passWord.sendKeys("1234");
loginButton.click();
}
public void selectEachPropertyAndSeachJob() {
WebElement dateRange = driver.findElement(By.xpath("//*[contains(@name,'date_range')]"));
WebElement last7days = driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]"));
WebElement searchJobs = driver.findElement(By.xpath("//*[contains(@name,'layout')]"));
WebElement propertyDropdown = driver.findElement(By.xpath("//*[contains(@id,'property_id')]"));
Select dropdown = new Select(propertyDropdown);
List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
if (propertyDropdown.isDisplayed() && propertyDropdown.isEnabled()) {
try {
propertyDropdown.click();
dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
dateRange.click();
last7days.click();
searchJobs.click();
System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());
} catch (org.openqa.selenium.StaleElementReferenceException ex) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(propertyDropdown));
}
}
}
}
public static void main(String args) {
MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
obj.getSetup();
obj.logIntoMobops();
obj.selectEachPropertyAndSeachJob();
}
}
Right now it driver just select the first option and clicks on submit. Right after the page loads after the search completes I get the following error:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
I have tried to implement the following code in my code, but since I am a novice, I have no idea how to implement the following to fix the issue:
new WebDriverWait(driver, timeout)
.ignoring(StaleElementReferenceException.class)
.until((WebDriver d) -> {
d.findElement(By.xpath("//*[contains(@id,'property_id')]")).click();
return true;
});
Any help to overcome this issue is greatly appreciated. Thanks.
java selenium staleelementreferenceexception
What I am trying to do is after logging in, driver will click on Property dropdown, select an option and click on submit and repeat the process until the loop is complete.
Below is my code:
package com.genericlibrary;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.util.Highlighter;
public class MobopsSearchJobsFromDropDown {
WebDriver driver;
Highlighter color;
public void getSetup() {
String path = System.getProperty("user.dir");
String driverPath = path + "\Driver\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("http://mobops-test.jcdecauxna.com/");
driver.manage().window().maximize();
}
public void logIntoMobops() {
WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));
userName.sendKeys("test2");
passWord.sendKeys("1234");
loginButton.click();
}
public void selectEachPropertyAndSeachJob() {
WebElement dateRange = driver.findElement(By.xpath("//*[contains(@name,'date_range')]"));
WebElement last7days = driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]"));
WebElement searchJobs = driver.findElement(By.xpath("//*[contains(@name,'layout')]"));
WebElement propertyDropdown = driver.findElement(By.xpath("//*[contains(@id,'property_id')]"));
Select dropdown = new Select(propertyDropdown);
List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
if (propertyDropdown.isDisplayed() && propertyDropdown.isEnabled()) {
try {
propertyDropdown.click();
dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
dateRange.click();
last7days.click();
searchJobs.click();
System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());
} catch (org.openqa.selenium.StaleElementReferenceException ex) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(propertyDropdown));
}
}
}
}
public static void main(String args) {
MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
obj.getSetup();
obj.logIntoMobops();
obj.selectEachPropertyAndSeachJob();
}
}
Right now it driver just select the first option and clicks on submit. Right after the page loads after the search completes I get the following error:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
I have tried to implement the following code in my code, but since I am a novice, I have no idea how to implement the following to fix the issue:
new WebDriverWait(driver, timeout)
.ignoring(StaleElementReferenceException.class)
.until((WebDriver d) -> {
d.findElement(By.xpath("//*[contains(@id,'property_id')]")).click();
return true;
});
Any help to overcome this issue is greatly appreciated. Thanks.
java selenium staleelementreferenceexception
java selenium staleelementreferenceexception
edited Nov 21 at 1:18
Koray Tugay
8,64826111219
8,64826111219
asked Nov 20 at 17:57
Syed Abdullah
164
164
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Someone helped me with the problem. Below is the solution:
public class MobopsSearchJobsFromDropDown {
WebDriver driver;
HighLighter color;
public void getSetup() throws Throwable {
String path = System.getProperty("user.dir");
String driverPath = path + "\driver\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("http://mobops-test.jcdecauxna.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
public void logIntoMobops() throws Throwable {
WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));
userName.sendKeys("test2");
passWord.sendKeys("1234");
loginButton.click();
}
public void selectEachPropertyAndSeachJob() throws Throwable {
WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> dropdownoptions = driver.findElements(By.xpath("//select[@id = 'property_id']//option"));
for( int i =0; i<dropdownoptions.size(); i++) {
String propertyDropdown = "//*[contains(@id,'property_id')]";
String dateRange = "//*[contains(@name,'date_range')]";
String last7days = "(//*[contains(text(),'Last 7 Days')])[2]";
String searchJobs = "//*[contains(@name,'layout')]";
Select dropdown = new Select(waitMethod(propertyDropdown));
WebElement option = dropdown.getOptions().get(i);
wait.until(ExpectedConditions.not(ExpectedConditions.stalenessOf(option)));
dropdown.selectByVisibleText(option.getText());
System.out.println(option.getText());
waitMethod(dateRange).click();
waitMethod(last7days).click();
waitMethod(searchJobs).click();
driver.navigate().refresh();
}
}
public WebElement waitMethod(String waiting) {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement waitForRefresh =wait.until(ExpectedConditions.refreshed(ExpectedConditions.presenceOfElementLocated(By.xpath(waiting))));
return waitForRefresh;
}
public static void main(String args) throws Throwable {
MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
obj.getSetup();
obj.logIntoMobops();
obj.selectEachPropertyAndSeachJob();
}
}
add a comment |
After you execute searchJobs.click(); your page reloads, so all references to previously found elements are lost.
You need to get all your WebElements, Select dropdown, and list of options again after searchJobs.click(); before using them second time.
So do not save webelements.
Something like this should work:
public void selectEachPropertyAndSeachJob() {
Select dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
if (driver.findElement(By.xpath("//*[contains(@id,'property_id')]")).isDisplayed()) {
//propertyDropdown.click(); no need to click
dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
driver.findElement(By.xpath("//*[contains(@name,'date_range')]")).click();
driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]")).click();
driver.findElement(By.xpath("//*[contains(@name,'layout')]")).click();
// Need to find it again
dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
optionsInPropertyDropdown = dropdown.getOptions();
System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());
}
}
}
Instead of using driver.findElement every time, you can save your locators as By elements and create custom find method.
Thank you. Just purchased your course.
– Syed Abdullah
Nov 21 at 15:37
I hope you will like it and learn new skills. If you have any questions while going through lectures, feel free to message me on udemy or leave your question in Q&A section.
– Dmitry
Nov 21 at 16:17
1
I certainly will. Thanks.
– Syed Abdullah
Nov 21 at 17:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398835%2fstale-element-reference-element-is-not-attached-to-the-page-document-for-java-s%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Someone helped me with the problem. Below is the solution:
public class MobopsSearchJobsFromDropDown {
WebDriver driver;
HighLighter color;
public void getSetup() throws Throwable {
String path = System.getProperty("user.dir");
String driverPath = path + "\driver\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("http://mobops-test.jcdecauxna.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
public void logIntoMobops() throws Throwable {
WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));
userName.sendKeys("test2");
passWord.sendKeys("1234");
loginButton.click();
}
public void selectEachPropertyAndSeachJob() throws Throwable {
WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> dropdownoptions = driver.findElements(By.xpath("//select[@id = 'property_id']//option"));
for( int i =0; i<dropdownoptions.size(); i++) {
String propertyDropdown = "//*[contains(@id,'property_id')]";
String dateRange = "//*[contains(@name,'date_range')]";
String last7days = "(//*[contains(text(),'Last 7 Days')])[2]";
String searchJobs = "//*[contains(@name,'layout')]";
Select dropdown = new Select(waitMethod(propertyDropdown));
WebElement option = dropdown.getOptions().get(i);
wait.until(ExpectedConditions.not(ExpectedConditions.stalenessOf(option)));
dropdown.selectByVisibleText(option.getText());
System.out.println(option.getText());
waitMethod(dateRange).click();
waitMethod(last7days).click();
waitMethod(searchJobs).click();
driver.navigate().refresh();
}
}
public WebElement waitMethod(String waiting) {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement waitForRefresh =wait.until(ExpectedConditions.refreshed(ExpectedConditions.presenceOfElementLocated(By.xpath(waiting))));
return waitForRefresh;
}
public static void main(String args) throws Throwable {
MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
obj.getSetup();
obj.logIntoMobops();
obj.selectEachPropertyAndSeachJob();
}
}
add a comment |
Someone helped me with the problem. Below is the solution:
public class MobopsSearchJobsFromDropDown {
WebDriver driver;
HighLighter color;
public void getSetup() throws Throwable {
String path = System.getProperty("user.dir");
String driverPath = path + "\driver\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("http://mobops-test.jcdecauxna.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
public void logIntoMobops() throws Throwable {
WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));
userName.sendKeys("test2");
passWord.sendKeys("1234");
loginButton.click();
}
public void selectEachPropertyAndSeachJob() throws Throwable {
WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> dropdownoptions = driver.findElements(By.xpath("//select[@id = 'property_id']//option"));
for( int i =0; i<dropdownoptions.size(); i++) {
String propertyDropdown = "//*[contains(@id,'property_id')]";
String dateRange = "//*[contains(@name,'date_range')]";
String last7days = "(//*[contains(text(),'Last 7 Days')])[2]";
String searchJobs = "//*[contains(@name,'layout')]";
Select dropdown = new Select(waitMethod(propertyDropdown));
WebElement option = dropdown.getOptions().get(i);
wait.until(ExpectedConditions.not(ExpectedConditions.stalenessOf(option)));
dropdown.selectByVisibleText(option.getText());
System.out.println(option.getText());
waitMethod(dateRange).click();
waitMethod(last7days).click();
waitMethod(searchJobs).click();
driver.navigate().refresh();
}
}
public WebElement waitMethod(String waiting) {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement waitForRefresh =wait.until(ExpectedConditions.refreshed(ExpectedConditions.presenceOfElementLocated(By.xpath(waiting))));
return waitForRefresh;
}
public static void main(String args) throws Throwable {
MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
obj.getSetup();
obj.logIntoMobops();
obj.selectEachPropertyAndSeachJob();
}
}
add a comment |
Someone helped me with the problem. Below is the solution:
public class MobopsSearchJobsFromDropDown {
WebDriver driver;
HighLighter color;
public void getSetup() throws Throwable {
String path = System.getProperty("user.dir");
String driverPath = path + "\driver\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("http://mobops-test.jcdecauxna.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
public void logIntoMobops() throws Throwable {
WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));
userName.sendKeys("test2");
passWord.sendKeys("1234");
loginButton.click();
}
public void selectEachPropertyAndSeachJob() throws Throwable {
WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> dropdownoptions = driver.findElements(By.xpath("//select[@id = 'property_id']//option"));
for( int i =0; i<dropdownoptions.size(); i++) {
String propertyDropdown = "//*[contains(@id,'property_id')]";
String dateRange = "//*[contains(@name,'date_range')]";
String last7days = "(//*[contains(text(),'Last 7 Days')])[2]";
String searchJobs = "//*[contains(@name,'layout')]";
Select dropdown = new Select(waitMethod(propertyDropdown));
WebElement option = dropdown.getOptions().get(i);
wait.until(ExpectedConditions.not(ExpectedConditions.stalenessOf(option)));
dropdown.selectByVisibleText(option.getText());
System.out.println(option.getText());
waitMethod(dateRange).click();
waitMethod(last7days).click();
waitMethod(searchJobs).click();
driver.navigate().refresh();
}
}
public WebElement waitMethod(String waiting) {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement waitForRefresh =wait.until(ExpectedConditions.refreshed(ExpectedConditions.presenceOfElementLocated(By.xpath(waiting))));
return waitForRefresh;
}
public static void main(String args) throws Throwable {
MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
obj.getSetup();
obj.logIntoMobops();
obj.selectEachPropertyAndSeachJob();
}
}
Someone helped me with the problem. Below is the solution:
public class MobopsSearchJobsFromDropDown {
WebDriver driver;
HighLighter color;
public void getSetup() throws Throwable {
String path = System.getProperty("user.dir");
String driverPath = path + "\driver\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("http://mobops-test.jcdecauxna.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
public void logIntoMobops() throws Throwable {
WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));
userName.sendKeys("test2");
passWord.sendKeys("1234");
loginButton.click();
}
public void selectEachPropertyAndSeachJob() throws Throwable {
WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> dropdownoptions = driver.findElements(By.xpath("//select[@id = 'property_id']//option"));
for( int i =0; i<dropdownoptions.size(); i++) {
String propertyDropdown = "//*[contains(@id,'property_id')]";
String dateRange = "//*[contains(@name,'date_range')]";
String last7days = "(//*[contains(text(),'Last 7 Days')])[2]";
String searchJobs = "//*[contains(@name,'layout')]";
Select dropdown = new Select(waitMethod(propertyDropdown));
WebElement option = dropdown.getOptions().get(i);
wait.until(ExpectedConditions.not(ExpectedConditions.stalenessOf(option)));
dropdown.selectByVisibleText(option.getText());
System.out.println(option.getText());
waitMethod(dateRange).click();
waitMethod(last7days).click();
waitMethod(searchJobs).click();
driver.navigate().refresh();
}
}
public WebElement waitMethod(String waiting) {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement waitForRefresh =wait.until(ExpectedConditions.refreshed(ExpectedConditions.presenceOfElementLocated(By.xpath(waiting))));
return waitForRefresh;
}
public static void main(String args) throws Throwable {
MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
obj.getSetup();
obj.logIntoMobops();
obj.selectEachPropertyAndSeachJob();
}
}
answered Nov 21 at 0:55
Syed Abdullah
164
164
add a comment |
add a comment |
After you execute searchJobs.click(); your page reloads, so all references to previously found elements are lost.
You need to get all your WebElements, Select dropdown, and list of options again after searchJobs.click(); before using them second time.
So do not save webelements.
Something like this should work:
public void selectEachPropertyAndSeachJob() {
Select dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
if (driver.findElement(By.xpath("//*[contains(@id,'property_id')]")).isDisplayed()) {
//propertyDropdown.click(); no need to click
dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
driver.findElement(By.xpath("//*[contains(@name,'date_range')]")).click();
driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]")).click();
driver.findElement(By.xpath("//*[contains(@name,'layout')]")).click();
// Need to find it again
dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
optionsInPropertyDropdown = dropdown.getOptions();
System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());
}
}
}
Instead of using driver.findElement every time, you can save your locators as By elements and create custom find method.
Thank you. Just purchased your course.
– Syed Abdullah
Nov 21 at 15:37
I hope you will like it and learn new skills. If you have any questions while going through lectures, feel free to message me on udemy or leave your question in Q&A section.
– Dmitry
Nov 21 at 16:17
1
I certainly will. Thanks.
– Syed Abdullah
Nov 21 at 17:58
add a comment |
After you execute searchJobs.click(); your page reloads, so all references to previously found elements are lost.
You need to get all your WebElements, Select dropdown, and list of options again after searchJobs.click(); before using them second time.
So do not save webelements.
Something like this should work:
public void selectEachPropertyAndSeachJob() {
Select dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
if (driver.findElement(By.xpath("//*[contains(@id,'property_id')]")).isDisplayed()) {
//propertyDropdown.click(); no need to click
dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
driver.findElement(By.xpath("//*[contains(@name,'date_range')]")).click();
driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]")).click();
driver.findElement(By.xpath("//*[contains(@name,'layout')]")).click();
// Need to find it again
dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
optionsInPropertyDropdown = dropdown.getOptions();
System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());
}
}
}
Instead of using driver.findElement every time, you can save your locators as By elements and create custom find method.
Thank you. Just purchased your course.
– Syed Abdullah
Nov 21 at 15:37
I hope you will like it and learn new skills. If you have any questions while going through lectures, feel free to message me on udemy or leave your question in Q&A section.
– Dmitry
Nov 21 at 16:17
1
I certainly will. Thanks.
– Syed Abdullah
Nov 21 at 17:58
add a comment |
After you execute searchJobs.click(); your page reloads, so all references to previously found elements are lost.
You need to get all your WebElements, Select dropdown, and list of options again after searchJobs.click(); before using them second time.
So do not save webelements.
Something like this should work:
public void selectEachPropertyAndSeachJob() {
Select dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
if (driver.findElement(By.xpath("//*[contains(@id,'property_id')]")).isDisplayed()) {
//propertyDropdown.click(); no need to click
dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
driver.findElement(By.xpath("//*[contains(@name,'date_range')]")).click();
driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]")).click();
driver.findElement(By.xpath("//*[contains(@name,'layout')]")).click();
// Need to find it again
dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
optionsInPropertyDropdown = dropdown.getOptions();
System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());
}
}
}
Instead of using driver.findElement every time, you can save your locators as By elements and create custom find method.
After you execute searchJobs.click(); your page reloads, so all references to previously found elements are lost.
You need to get all your WebElements, Select dropdown, and list of options again after searchJobs.click(); before using them second time.
So do not save webelements.
Something like this should work:
public void selectEachPropertyAndSeachJob() {
Select dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
if (driver.findElement(By.xpath("//*[contains(@id,'property_id')]")).isDisplayed()) {
//propertyDropdown.click(); no need to click
dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
driver.findElement(By.xpath("//*[contains(@name,'date_range')]")).click();
driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]")).click();
driver.findElement(By.xpath("//*[contains(@name,'layout')]")).click();
// Need to find it again
dropdown = new Select(driver.findElement(By.xpath("//*[contains(@id,'property_id')]")));
optionsInPropertyDropdown = dropdown.getOptions();
System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());
}
}
}
Instead of using driver.findElement every time, you can save your locators as By elements and create custom find method.
edited Nov 23 at 15:28
meagar♦
177k29272288
177k29272288
answered Nov 21 at 1:03
Dmitry
35427
35427
Thank you. Just purchased your course.
– Syed Abdullah
Nov 21 at 15:37
I hope you will like it and learn new skills. If you have any questions while going through lectures, feel free to message me on udemy or leave your question in Q&A section.
– Dmitry
Nov 21 at 16:17
1
I certainly will. Thanks.
– Syed Abdullah
Nov 21 at 17:58
add a comment |
Thank you. Just purchased your course.
– Syed Abdullah
Nov 21 at 15:37
I hope you will like it and learn new skills. If you have any questions while going through lectures, feel free to message me on udemy or leave your question in Q&A section.
– Dmitry
Nov 21 at 16:17
1
I certainly will. Thanks.
– Syed Abdullah
Nov 21 at 17:58
Thank you. Just purchased your course.
– Syed Abdullah
Nov 21 at 15:37
Thank you. Just purchased your course.
– Syed Abdullah
Nov 21 at 15:37
I hope you will like it and learn new skills. If you have any questions while going through lectures, feel free to message me on udemy or leave your question in Q&A section.
– Dmitry
Nov 21 at 16:17
I hope you will like it and learn new skills. If you have any questions while going through lectures, feel free to message me on udemy or leave your question in Q&A section.
– Dmitry
Nov 21 at 16:17
1
1
I certainly will. Thanks.
– Syed Abdullah
Nov 21 at 17:58
I certainly will. Thanks.
– Syed Abdullah
Nov 21 at 17:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398835%2fstale-element-reference-element-is-not-attached-to-the-page-document-for-java-s%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown