找不到selenium webdriver元素

时间:2015-12-14 16:23:10

标签: javascript selenium xpath selenium-webdriver css-selectors

我正在尝试使用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("vzwpowertest1@gmail.com");

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



}

1 个答案:

答案 0 :(得分:1)

如果元素不可见,则添加等待元素存在。有'isDisplayed'的方法。您还可以在ExpectedConditions中使用visibilityofElement。 对于例如。

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

使用WebDriverWait

如果两者都不起作用,则问题出在定位器中。在页面上添加断点,并检查是否找到具有此定位器的元素。