我试图为我的新程序模拟几次点击,但我还是坚持了最后一件事!
我设法让selenium打开页面,然后点击复选框,其中显示一个矩形弹出框,里面有9个按钮。唯一的问题是点击弹出窗口内的按钮!我已经检查了几次xpath,但是Selenium说"没有这样的元素"
这是我的代码:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class test {
static WebDriver driver;
public static void main(String[] args) {
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
driver = new FirefoxDriver(myprofile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.runelocus.com/top-rsps-list/vote-1858-GrinderScape%20-%20New%20Website%20and%20Great%20Updates!/");
driver.switchTo().frame(1);
driver.findElement(By.xpath("//*[@id='recaptcha-anchor']/div[5]")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.cssSelector("#rc-imageselect-target > table > tbody > tr:nth-child(1) > td:nth-child(2)")).click();
}
}
修改 单击复选框后,它会创建新元素。我该如何点击它们?
答案 0 :(得分:3)
您尝试点击的元素位于iframe中。我们需要明确切换到iflen的iframe以找到该元素。下面的代码对我有用。 (请以更易读的格式格式化Xpath。)
driver.get("http://www.runelocus.com/top-rsps-list/vote-1858-GrinderScape%20-%20New%20Website%20and%20Great%20Updates!/");
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
WebElement iframeSwitch = driver.findElement(By.xpath("/html/body/section/div/div/section/div/article/div/div[2]/form/table/tbody/tr/td[1]/div/div/div/iframe"));
driver.switchTo().frame(iframeSwitch);
System.out.println("Switched");
driver.findElement(By.cssSelector("div[class=recaptcha-checkbox-checkmark]")).click();