0
下面是我的代碼: -無法在幀定位元素
package Practice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Day6FramesRecap {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/recaptcha/api2/demo");
int framenumber = frameset(driver,By.xpath(".//*[@id='recaptcha-anchor']/div[5]"));
driver.switchTo().frame(framenumber);
driver.findElement(By.xpath(".//*[@id='recaptcha-anchor']/div[5]")).click();
driver.switchTo().defaultContent();
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
int framenumber2 = frameset(driver,By.xpath(".//*[@id='recaptcha-verify-button']"));
driver.switchTo().frame(framenumber2);
driver.findElement(By.xpath(".//*[@id='recaptcha-verify-button']")).click();
}
public static int frameset(WebDriver driver, By by)
{
int i;
int framecount= driver.findElements(By.tagName("iframe")).size();
for(i=0;i<framecount;i++)
{
driver.switchTo().frame(i);
int count = driver.findElements(by).size();
if(count>0)
{
break;
}
else
{
System.out.println("Continue Looping");
}
}
driver.switchTo().defaultContent();
return i;
}
}
什麼,我試圖做的是,使用for循環通過每幀進行迭代,找到我所需要的元素。 但是,我能找到的第一個元素,即By.xpath(".//*[@id='recaptcha-anchor']/div[5]
並單擊在此之後,我不能夠點擊第二個元素By.xpath(".//*[@id='recaptcha-verify-button']
我遇到一個錯誤,指出: - 螺紋
異常「main」 org.openqa.selenium.NoSuchElementException:無法找到 元素: {「method」:「xpath」,「selector」:「.//*[@id ='recaptcha-verify-button']」 }
我沒有意識到這種方法..謝謝。 – Khan