I was trying to get some elements from this page: https://www.humblebundle.com/store/shadowgrounds-survivor so I could do something with them. When I inspected the page from Chrome or Firefox I would get every element in the page, or at least I thought so, but when trying to find the element with selenium it just didn't.
I realised the page was loading forever so I need to know if there's any possible way to do something with selenium even if the page isn't fully loaded.
This is my code:
public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\WebDriver\\bin\\geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
try {
driver.get("https://www.humblebundle.com/store/shadowgrounds-survivor");
WebElement listElement1 = driver.findElement(By.tagName(".current-price"));
} finally {
driver.close();
}
}
I get the NoSuchElementException from selenium.
("marionette", true)
the reason is hereBy.tagName(".current-price")
. Its a class name not tag name so use className
selector driver.findElement(By.className(".current-price"))
Implicit
or Explicit
wait in you script to manage timeout in your scriptsSo now you code become:
public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\WebDriver\\bin\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.humblebundle.com/store/shadowgrounds-survivor");
// handle age confirmation on above suggested page
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(".current-price")));
driver.get("https://www.humblebundle.com/store/shadowgrounds-survivor");
WebElement listElement1 = driver.findElement(By.className(".current-price"));
System.out.println(listElement1.getText());
driver.close();
}
You can make use of Selenium wait commands where you can wait for time interval for page to load.
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.