简体   繁体   English

Selenium 找不到任何元素

[英]Selenium can't locate ANY element

So I have a webscraper which first of all needs to get past the cookie banner of a given website.所以我有一个 webscraper,它首先需要通过给定网站的 cookie 横幅。 Normally I'd just locate the element by id or classname and be done with it, but on this site none of the elements can be located.通常我只是通过 id 或 classname 定位元素并完成它,但在这个站点上没有任何元素可以被定位。 I've tried/checked the following:我已经尝试/检查了以下内容:

  • The element of interest is <div id="cookiescript_accept" tabindex="0" role="button" data-cs-i18n-text="[]">Alles accepteren</div>感兴趣的元素是<div id="cookiescript_accept" tabindex="0" role="button" data-cs-i18n-text="[]">Alles accepteren</div>
  • The element is not part of an iframe该元素不是 iframe 的一部分
  • The element is not part of a shadow DOM该元素不是影子 DOM 的一部分
  • Using wait.until(ExpectedConditions.visibilityOfElementLocated hits the 15 seconds timeout使用wait.until(ExpectedConditions.visibilityOfElementLocated达到 15 秒超时
  • Using driver.executeScript("return document.getElementById('cookiescript_accept');");使用driver.executeScript("return document.getElementById('cookiescript_accept');"); doesn't work either也不起作用
  • The parent element and parent of parent can also not be found也找不到父元素和父元素的父元素

I'm still fairly new to Selenium and HTML so I must be missing something, please tell me if you know what that is我对 Selenium 和 HTML 还是很陌生,所以我一定遗漏了一些东西,如果你知道那是什么,请告诉我

Code:代码:

public void loadUrl(String url) {
    System.out.println("\t\t- loadUrl " + url);
    idle5000();

    driver.get(url);
    idle5000();

    setWindowSize();
    idle5000();
    printFirefoxCPU();

    scrollViewport();
    idle5000();
    printFirefoxCPU();
}
loadUrl("https://www.schoolplaten.com/");
prepRunnable.getDriver().findElement(By.id("cc-cookiescript_accept")).click();
//  -> NoSuchElementException
WebDriverWait wait = new WebDriverWait(prepRunnable.getDriver(), 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("cookiescript_accept")));
//  -> Timeout
WebElement elem = (WebElement) prepRunnable.getDriver().executeScript("return document.getElementById('cookiescript_accept');");
elem.click();
//  -> elem is null

I could locate the web element with the below XPath我可以使用下面的 XPath 找到 web 元素

//*[name()='div' and @id='cookiescript_accept']

and could perform click on it with the help of below code :并可以在以下代码的帮助下执行点击:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[name()='div' and @id='cookiescript_accept']"))).click();

To click() on the element ALLES ACCEPTEREN you can use either of the following Locator Strategies :要在元素ALLES ACCEPTERENclick()您可以使用以下任一定位器策略

  • cssSelector : cssSelector

     driver.findElement(By.cssSelector("div#cookiescript_accept")).click();
  • xpath : xpath

     driver.findElement(By.xpath("//div[@id='cookiescript_accept']")).click();

However, the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies :但是,该元素是一个动态元素,因此要在元素上click() ,您需要为elementToBeClickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • cssSelector : cssSelector

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#cookiescript_accept"))).click();
  • xpath : xpath

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='cookiescript_accept']"))).click();

Alternative选择

As an alternative you can also use the executeScript() as follows:作为替代方案,您还可以使用executeScript() ,如下所示:

  • cssSelector : cssSelector

     ((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#cookiescript_accept"))));
  • xpath : xpath

     ((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='cookiescript_accept']"))));

Reference参考

You can find a detailed discussion on NoSuchElementException in:您可以在以下位置找到有关NoSuchElementException的详细讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM