2015-12-14 230 views
-2

我试图通过使用Selenium webdriver访问网页,但我被卡住以找出元素。找不到selenium webdriver元素

WebElement button3 = driver.findElement(
    By.cssSelector(".device-selector-dropdown-child.device-selected")); 

我使用CSS,xpath,但他们都没有工作。

package org.openqa.selenium.chrome; 

//import java.util.Scanner; 
import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
//import org.openqa.selenium.JavascriptExecutor; 
//import org.openqa.selenium.Keys; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 



public class marslogin{ 
static WebDriver driver; 
static WebDriver document; 

public static void main(String[] args) throws InterruptedException { 


    System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); 

    driver = new ChromeDriver(); 
    //driver = new FirefoxDriver(); 


    driver.get("https://accounts.google.com/ServiceLogin?"); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

    //WebElement id = driver.findElement(By.name("username")); 
    WebElement id = driver.findElement(By.name("Email")); 
    id.sendKeys("[email protected]"); 

    WebElement button = driver.findElement(By.name("signIn")); 
    button.click(); 

    WebElement pass = driver.findElement(By.id("Passwd")); 
    pass.sendKeys("battery123"); 

    WebElement button1 = driver.findElement(By.id("signIn")); 
    button1.click(); 

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    //driver.get("https://play.google.com/store/apps/collection/topselling_free"); 
    //driver.manage().window().maximize(); 
    driver.get("https://play.google.com/store/apps/details?id=com.facebook.orca"); 
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
    Thread.sleep(1000); 
    WebElement button2 = driver.findElement(By.xpath("//*[@id='body-content']/div/div/div[1]/div[1]/div/div[1]/div/div[3]/div/div[1]/span/span/span/button[2]")); 
    button2.click(); 
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
    WebElement button3 = driver.findElement(By.cssSelector(".device-selector-dropdown-child.device-selected")); 
    button3.click(); 
    //WebElement button4 = driver.findElement(By.xpath("//*[@id='device-selector-container']/div/div/div/button[2]")); 
    //button4.click(); 
    //WebElement button5 = driver.findElement(By.xpath("//*[@id='purchase-ok-button']")); 
    // button5.click(); 
    //WebElement button6 = driver.findElement(By.cssSelector("#close-dialog-button")); 
    //button6.click(); 



} 
+0

从代码 – drets

+0

@ drets 21删除个人数据,如gmail地址和密码,这不是个人数据。它只是测试帐户。 –

+0

任何人请帮助 –

回答

1

如果该元素不可见,则添加一个等待元素存在。这是'isDisplayed'的方法。您也可以在ExpectedConditions中使用visibilityofElement。例如:

driver.findElement(foo.bar).isDisplayed(); 

使用WebDriverWait

如果这两个不工作,那么问题出在定位器。在页面上添加一个断点并检查是否找到具有此定位器的元素。

+0

请给出答案的例子。 – Sagar007

相关问题