英文:
Is there any solution to bypass the cookie iframe by using selenium webdriver?
问题
我在以下嵌入了一个iframe的网站上遇到了接受cookie警报的问题:
我尝试了很多种方法来解决使用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:
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>,因为所需元素位于<iframe>
中,所以您需要:
-
使用 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();
-
-
浏览器快照:
参考
您可以在以下讨论中找到一些相关内容:
英文:
To click on <kbd>Alle akzeptieren</kbd> within the url https://www.hamburg.de/, as the the desired element is within a <iframe>
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("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();
-
Using 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();
-
-
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论