2015-11-04 52 views
-2

我试图模拟几个点击为我的新程序,但我坚持最后一件事!无法让硒单击一个元素

我已经设法让硒打开页面,然后点击显示一个矩形弹出框的复选框,其中有9个按钮。唯一的问题是点击弹出窗口中的按钮!我已检查的XPath了几次,但硒说“没有这样的元素”

这里是我的代码:

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(); 
    } 


} 

链接的问题:http://www.runelocus.com/top-rsps-list/vote-1858-GrinderScape%20-%20New%20Website%20and%20Great%20Updates!/

编辑: 它创建点击复选框后的新元素。我将如何点击它们?

+1

您正试图制作一个机器人,点击“我不是机器人复选框”? – odedsh

+0

您需要切换到该弹出窗口。因为它可能是新窗口。 – MKay

+0

是的,然后我屏幕截图验证码并发送到验证码解决服务。 – joe

回答

3

您尝试点击的元素位于iframe中。我们需要明确地切换到该硒的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(); 
+0

感谢您的回复。我已经切换了框架,并且可以点击复选框,只需点击复选框后的弹出窗口。我需要点击弹出窗口中的9个图像之一。 – joe

+0

我是否缺少弹出窗口?不知何故,我无法看到带有9个复选框的窗口。当你点击'我不是机器人'时,它就停留在页面上。 – MKay

+0

没关系我设法解决它。不过谢谢。 – joe