有没有办法通过使用Selenium WebDriver绕过cookie iframe?

huangapple go评论69阅读模式
英文:

Is there any solution to bypass the cookie iframe by using selenium webdriver?

问题

我在以下嵌入了一个iframe的网站上遇到了接受cookie警报的问题:

https://www.hamburg.de/

我尝试了很多种方法来解决使用driver.switchTo().frame()方法的问题:

  • 通过使用iframe的Id sp_message_iframe_234327
  • 通过为iframe插入一个索引 0
  • 通过使用WebElement调用iframe
WebElement element = driver.findElement(By.xpath("//body/div[6]/iframe[@src='https://cdn.privacy-mgmt.com/index.html?message_id=234327&consentUUID=b2cb90ea-dfdd-4655-b6b9-89117ff34893&requestUUID=ccb96546-c6b5-44e7-9869-438b32f7ad89&preload_message=true']"));
driver.switchTo().frame(element);

不幸的是,它们都没有起作用。我总是得到以下异常:

org.openqa.selenium.NoSuchFrameException

有谁对这个特定的示例有一个切换到iframe的正确方法吗?
我正在使用Selenium WebDriver 3.141.59进行Java开发,我的测试应在Mozilla Firefox (80.0.1)和Chromium (85.0.4183.102)上执行。这两个浏览器都以无头方式启动。对于任何帮助感到高兴。

英文:

I have an issue to accept a cookie alert on the below website which is embedded in an iframe:

https://www.hamburg.de/

有没有办法通过使用Selenium WebDriver绕过cookie iframe?

I've tried many ways to solve the issue with using the driver.switchTo().frame() - method:

  • by using the Id of the iframe sp_message_iframe_234327
  • by inserting an index for the iframe 0
  • by calling the iframe through a WebElement
    WebElement element = driver.findElement(By.xpath("//body/div[6]/iframe[@src='https://cdn.privacy-mgmt.com/index.html?message_id=234327&consentUUID=b2cb90ea-dfdd-4655-b6b9-89117ff34893&requestUUID=ccb96546-c6b5-44e7-9869-438b32f7ad89&preload_message=true']"));
    driver.switchTo().frame(element);

Unfortunately none of them are working. I'm always getting the following exception:

org.openqa.selenium.NoSuchFrameException

Does anyone have an idea on this specific example to switch on the iframe properly?
I'm working with Selenium WebDriver 3.141.59 on Java and my tests should be executed on Mozilla Firefox (80.0.1) and Chromium (85.0.4183.102). Those two browsers are launched headlessly.
Glad for any help.

答案1

得分: 2

点击位于URL https://www.hamburg.de/ 中的<kbd>Alle akzeptieren</kbd>,因为所需元素位于&lt;iframe&gt;中,所以您需要:

  • 使用 WebDriverWait 等待所需的 frameToBeAvailableAndSwitchToIt

  • 使用 WebDriverWait 等待所需的 elementToBeClickable

  • 您可以使用以下任一定位策略

    • 使用 cssSelector

      driver.get("https://www.hamburg.de/");
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='sp_message_iframe']")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[aria-label='Alle akzeptieren']"))).click();
      
    • 使用 xpath

      driver.get("https://www.hamburg.de/");
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@id, 'sp_message_iframe')]")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@aria-label='Alle akzeptieren']"))).click();
      
  • 浏览器快照:

有没有办法通过使用Selenium WebDriver绕过cookie iframe?


参考

您可以在以下讨论中找到一些相关内容:

英文:

To click on <kbd>Alle akzeptieren</kbd> within the url https://www.hamburg.de/, as the the desired element is within a &lt;iframe&gt; so you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.

  • Induce WebDriverWait for the desired elementToBeClickable.

  • You can use either of the following Locator Strategies:

    • Using cssSelector:

      driver.get(&quot;https://www.hamburg.de/&quot;);
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(&quot;iframe[id^=&#39;sp_message_iframe&#39;]&quot;)));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;button[aria-label=&#39;Alle akzeptieren&#39;]&quot;))).click();
      
    • Using xpath:

      driver.get(&quot;https://www.hamburg.de/&quot;);
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(&quot;//iframe[starts-with(@id, &#39;sp_message_iframe&#39;)]&quot;)));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//button[@aria-label=&#39;Alle akzeptieren&#39;]&quot;))).click();
      
  • Browser Snapshot:

有没有办法通过使用Selenium WebDriver绕过cookie iframe?


Reference

You can find a couple of relevant discussions in:

huangapple
  • 本文由 发表于 2020年9月10日 22:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63831444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定