簡體   English   中英

Selenium Webdriver無法使用標記名識別頁面上的鏈接

[英]Selenium webdriver unable to identify links on a page using tagname

Selenium將頁面上的鏈接數顯示為0,盡管頁面上有很多鏈接。

這是我在Java中的代碼

dr.get("https://www.ebay.com");
List<WebElement> linksize = dr.findElements(By.tagName("a"));
System.out.println(linksize.size());

輸出:0

等待頁面加載鏈接

修改您的代碼為

dr.get("https://www.ebay.com");
waitForLoad(dr); // Here you are calling the below method
List<WebElement> linksize = dr.findElements(By.tagName("a"));
System.out.println(linksize.size());

您可以使用以下方法作為util,並可以隨時調用

void waitForLoad(WebDriver driver) {
    ExpectedCondition<Boolean> pageLoadCondition = new
        ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
        };
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(pageLoadCondition);
}

@Subhrapratim Bhattacharjee,看來您需要等待頁面加載。 嘗試以下代碼

WebDriver driver = new FirefoxDriver();
    driver.get("https://www.ebay.com");
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    WebDriverWait wait = new WebDriverWait(driver, 30);
    List<WebElement> linksize = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName("a")));//driver.findElements(By.tagName("a"));
    System.out.println(linksize.size());

暫無
暫無

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

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