簡體   English   中英

Java - Selenium 加載新頁面時 ChromeDriver 掛起

[英]Java - Selenium ChromeDriver hangs out when new page is loaded

我正在實施一個腳本,該腳本登錄到 web 頁面並執行某些操作,這些操作在本例中無關緊要。 這是執行登錄的代碼:

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();

機器人成功登錄,登錄后,如果我想執行任何指令,我會得到非常奇怪的行為。 如果我嘗試在主頁中搜索任何項目,它不會找到它,因為它尚未加載,例如在下面添加此指令:

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

但是,如果我添加隱式等待:

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

或者顯式等待:

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

腳本掛起,不再執行任何指令。 例如,如果我添加以下打印,它永遠不會被執行:

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"));

行為很奇怪,好像頁面加載后 selenium 不起作用,只有在新頁面仍在加載且我要使用的 HTML 對象尚不可用時它才起作用。 我正在使用以下版本:

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

我無法指出我正在使用的 web 頁面的具體數據,因為它被檢測為垃圾郵件,但我之前已經確保我在腳本上搜索的項目存在於 HTML 代碼中,事實上,該腳本有效直到幾個月前才正確。

編輯:

我添加了一些 output 的示例以更好地理解問題。 如果我執行指令driver.findElement(By.id("my_panel_page")); 在不使用任何類型的等待的情況下,output 如下所示:

... (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}

如果我使用某種等待我沒有得到 output,ChromeDriver 初始化並成功登錄到 web 頁面,但是當它到達等待語句時它的執行掛起。 它只打印 ChromeDriver 初始化信息:

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

presenceOfElementLocated()

presenceOfElementLocated()是檢查元素是否存在於頁面的 DOM 上的期望。 這並不一定意味着該元素是可見的。


解決方案

因此,您必須為visibilityOfElementLocated()引入WebDriverWait而不是presenceOfElementLocated() ,您可以使用以下解決方案:

因此,您必須為visibilityOfElementLocated()引入WebDriverWait而不是presenceOfElementLocated() ,您可以使用以下解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM