0

I am implementing a script that logs into a web page and perform certain actions, that are irrelevant in this case. This is the code that executes the login:

WebDriver driver;
JavascriptExecutor exec;

String dni = "...";
String passwd = "...";

driver = new ChromeDriver();
exec = (JavascriptExecutor) driver;

// Search web page
driver.get("http://example.com");

// Accept cookies
driver.findElement(By.id("aceptarCookies")).click();


// Login
driver.findElement(By.id("areaPersonal")).click();
driver.findElement(By.id("tab_login_user")).sendKeys(dni);
driver.findElement(By.id("tab_login_password")).sendKeys(passwd);
driver.findElement(By.id("loginFormHeader")).findElement(By.id("buttonHeader")).click();

The bot logs in successfully and, once logged in, if I want to execute any instruction I get very strange behaviors. If I try to search for any item within the home page it doesn't find it because it hasn't been loaded yet, for example adding this instruction just below:

// Search body item
driver.findElement(By.id("my_panel_page"));

However, if I add an implicit wait:

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Search body item
driver.findElement(By.id("my_panel_page"));

Or an explicit wait:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id_of_new_item")));
driver.findElement(By.id("my_panel_page"));

The script hangs and does not execute any more instructions. For example, if I add the following print, it never gets executed:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id_of_new_item")));
System.out.println("Page loaded!!!");
driver.findElement(By.id("my_panel_page"));

The behavior is very strange, it is as if selenium does not work once the page has been loaded, it only works when the new page is still loading and the HTML objects I want to use are not yet available. I'm using the following versions:

  • ChromeDriver: 109.0.5414.74
  • Chrome: 109.0.5414.120
  • Selenium: 3.141.59
  • JDK: 11.0.16.1

I can't indicate specific data of the web page I'm using because it is detected as spam, but I have previously made sure that the items I'm searching on the script exists in the HTML code, in fact, the script worked correctly until a few months ago.

EDIT:

I add some examples of output to better understand the problem. If I execute the instruction driver.findElement(By.id("my_panel_page")); without using any type of wait the output is as follows:

... (Initialize ChromeDriver)
    
no such element: Unable to locate element: {"method":"css selector","selector":"#my_panel_page"}
  (Session info: chrome=109.0.5414.120)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'xxx', ip: 'xxx', os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.16.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 109.0.5414.120, chrome: {chromedriverVersion: 109.0.5414.74 (e7c5703604da..., userDataDir: C:\Users\xxx\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:52373}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 533a8a11a33254b8ab788522dada77ab
*** Element info: {Using=id, value=my_panel_page}

If I use some kind of wait I didn't get an output, the ChromeDriver initializes and logs into the web page successfully, but its execution hangs when it reaches the wait statement. It only prints the ChromeDriver initialization info:

Starting ChromeDriver 109.0.5414.74 (e7c5703604daa9cc128ccf5a5d3e993513758913-refs/branch-heads/5414@{#1172}) on port 15136
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
feb. 01, 2023 5:28:28 P. M. org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMACIËN: Detected dialect: W3C
TomeMD
  • 1
  • 3
  • 1
    Are errors thrown when trying to locate the item? – Andrew Ryan Jan 31 '23 at 18:38
  • Please edit your question and post the error message (as plain text, properly formatted) you are receiving. Have you checked to see if the elements are contained in an IFRAME? Are you sure that the page isn't just taking a long time to load, e.g. more than your 20s wait? – JeffC Feb 01 '23 at 05:30
  • You only get an error when trying to locate the element without using an implicit or explicit wait, because the element has not yet been loaded when I try to search for it. The problem is that adding any kind of wait causes the script to hang without returning any error. I have also tried increasing the timeout but it does not help, page loads in about 17 seconds. – TomeMD Feb 01 '23 at 09:56

1 Answers1

0

presenceOfElementLocated()

presenceOfElementLocated() is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.


Solution

So instead of presenceOfElementLocated(), you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following solution:

So instead of presenceOfElementLocated(), you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following solution:

try 
{
    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.id("id_of_new_item")));
    System.out.println("new_item is visible");
    System.out.println("Page loaded!!!");
    driver.findElement(By.id("my_panel_page"));
    // lines of the program
}
catch(TimeoutException e) {
    System.out.println("new_item is not visible");
}
// remaining lines of the program
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I have tried changing the ExepectedCondition but the problem persists, the instruction `System.out.println("Page loaded!");` never gets executed. – TomeMD Feb 01 '23 at 09:51
  • I checked out the updated answer and i didn't get any output, after 5 minutes of execution the script doesn`t prints nothing. Output: ```java Starting ChromeDriver 109.0.5414.74 (e7c5703604daa9cc128ccf5a5d3e993513758913-refs/branch-heads/5414@{#1172}) on port 40635 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully. feb. 01, 2023 5:18:16 P. M. org.openqa.selenium.remote.ProtocolHandshake createSession INFORMACIËN: Detected dialect: W3C ``` – TomeMD Feb 01 '23 at 16:21