1

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.

JeffC
  • 22,180
  • 5
  • 32
  • 55
jose
  • 11
  • 3

2 Answers2

0
  1. There is no need to set capabilities ("marionette", true) the reason is here
  2. Selector is wrong By.tagName(".current-price"). Its a class name not tag name so use className selector driver.findElement(By.className(".current-price"))
  3. While open the link you shared, it ask for confirm the age as shown below So you need to first handle this

enter image description here

  1. Introduce Implicit or Explicit wait in you script to manage timeout in your scripts

So 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();
}
NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • @jose, Does this answer resolve you issue ? If yes then please accept the answer by click on tick mark below the vote count on answer. So it can be helpful for others. If no then update your question with more details or feel free to ask in comments. Thanks :) – NarendraR Mar 05 '20 at 12:10
-1

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>));
Ananth
  • 787
  • 5
  • 18
  • This won't do anything if the page isn't done loading. – JeffC Mar 02 '20 at 05:54
  • If there is no element on the page, then you can't do anything than to wait for the element to load. – Ananth Mar 02 '20 at 06:37
  • There could be many elements on the page. In fact the entire page could be loaded but some process could be running in the background that the browser is waiting to finish. Selenium won't try to access the page until the browser signals that it's done loading. – JeffC Mar 02 '20 at 14:35