2015-11-04 89 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