Java 在iFrame中找不到元素
我正在尝试获取某个按钮/框架的xpath。我能够使用xpath定位元素,但是在运行自动化时,我得到了元素NotFoundException。我试图切换到特定的帧,然后找到按钮,但它没有工作。附加页面和图像的链接。 链接: 这是我试过的代码Java 在iFrame中找不到元素,java,selenium,selenium-webdriver,iframe,Java,Selenium,Selenium Webdriver,Iframe,我正在尝试获取某个按钮/框架的xpath。我能够使用xpath定位元素,但是在运行自动化时,我得到了元素NotFoundException。我试图切换到特定的帧,然后找到按钮,但它没有工作。附加页面和图像的链接。 链接: 这是我试过的代码 WebDriverWait wait = new WebDriverWait(cdriver,30); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='foot'
WebDriverWait wait = new WebDriverWait(cdriver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='foot']//following::iframe[@style='width:9.7rem;']")));
System.out.println("Ive waited for the frame to load");
int size = cdriver.findElements(By.tagName("iframe")).size();
System.out.println(size);
/*for(int i=0; i<=size; i++){
cdriver.switchTo().frame(i);
System.out.println("Switched to frame "+i);
int total= cdriver.findElements(By.xpath("//*[@id='u_0_0']/div/button/span")).size();
System.out.println(total);
cdriver.switchTo().defaultContent();
}*/
cdriver.switchTo().frame(2);
System.out.println("Switched to frame");
cdriver.findElement(By.xpath("//*[@id='u_0_0']/div/button/span")).click();
WebDriverWait wait=newwebdriverwait(cdriver,30);
wait.until(ExpectedConditions.elementtobelickable(By.xpath(“//div[@id='foot']//following::iframe[@style='width:9.7rem;']));
System.out.println(“我等待帧加载”);
int size=cdriver.findElements(按.tagName(“iframe”)).size();
系统输出打印项次(尺寸);
/*为了(int i=0;i访问所需元素,因为所需元素位于
中,因此您必须:
- 为所需的
框架导入WebDriverWait以使其可用并切换到it()
- 您可以使用以下任一选项:
- 使用
CSS\u选择器
:
driver.get("https://www.msn.com/en-in/weather/today/New-Delhi,Delhi,India/we-city-28.608,77,201?iso=IN")
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src^='//www.facebook.com/plugins/like']")));
- 使用
XPATH
:
driver.get("https://www.msn.com/en-in/weather/today/New-Delhi,Delhi,India/we-city-28.608,77,201?iso=IN")
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@src, '//www.facebook.com/plugins/like')]")));
在这里,您可以找到关于
要访问所需元素,因为所需元素位于
中,因此您必须:
- 为所需的
框架导入WebDriverWait以使其可用并切换到it()
- 您可以使用以下任一选项:
- 使用
CSS\u选择器
:
driver.get("https://www.msn.com/en-in/weather/today/New-Delhi,Delhi,India/we-city-28.608,77,201?iso=IN")
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src^='//www.facebook.com/plugins/like']")));
- 使用
XPATH
:
driver.get("https://www.msn.com/en-in/weather/today/New-Delhi,Delhi,India/we-city-28.608,77,201?iso=IN")
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@src, '//www.facebook.com/plugins/like')]")));
在这里,您可以找到关于
当您使用switchTo().frame(2)
时,您实际上切换到了第三个
。您可以使用switchTo().frame(1)
,但更好的选择是使用唯一标识符而不是索引
WebElement iframe = cdriver.findElement(By.csSelector("#fbcount > iframe"));
cdriver.switchTo().frame(iframe);
要等待加载帧,请使用frametobeavailable和switchtoit
,而不是element tobeclickable
wait.until(ExpectedConditions.elementToBeClickable(By.csSelector("#fbcount > iframe")));
System.out.println("Switched to frame");
当您使用switchTo().frame(2)
时,您实际上切换到了第三个
。您可以使用switchTo().frame(1)
,但更好的选择是使用唯一标识符而不是索引
WebElement iframe = cdriver.findElement(By.csSelector("#fbcount > iframe"));
cdriver.switchTo().frame(iframe);
要等待加载帧,请使用frametobeavailable和switchtoit
,而不是element tobeclickable
wait.until(ExpectedConditions.elementToBeClickable(By.csSelector("#fbcount > iframe")));
System.out.println("Switched to frame");
他在用Java,不是Python。@Guy Good catch:)他在用Java,不是Python。@Guy Good catch:)