Selenium gecko浏览器无法找到元素

时间:2017-07-25 02:41:21

标签: selenium selenium-webdriver geckodriver

不确定原因,但似乎在Gecko Browser中找到该元素时出错。在Chrome中完美运行。尝试了不同的变化。所以工作似乎只有50%然后停止

这里是:        package src;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ha {

    public static void main(String[] args) {

    // declaration and instantiation of objects/variables
    WebDriver driver ;
    System.setProperty("webdriver.gecko.driver","C:\\Users\\Bain1\\eclipse-workspace\\New Project\\geckodriver.exe");
    driver = new FirefoxDriver();

    //Create explicit wait.
    WebDriverWait myWait = new WebDriverWait(driver, 10);

    String baseUrl = "http://newtours.demoaut.com/";
    driver.get(baseUrl);

    //wait until text box load.
    myWait.until(ExpectedConditions.visibilityOfElementLocated(By.name("userName")));

    // Enter value into textbox
    driver.findElement(By.name("userName")).sendKeys ("tutorial");

    // Delete value from textbox
    driver.findElement(By.name("userName")).clear();

    // Access Links
    driver.findElement(By.linkText("Register here")).click();
    driver.findElement(By.partialLinkText("SIGN-ON")).click();

    // Enter value into textbox and SUBMIT
    driver.findElement(By.name("userName")).sendKeys ("tutorial");
    driver.findElement(By.name("password")).sendKeys ("tutorial");
    driver.findElement(By.name("password")).submit();

    // Select Radio Button
    driver.findElement(By.cssSelector("input[value=Business]")).click();

    //Select CheckBox
    String baseURL = "http://demo.guru99.com/sele...";
    driver.get(baseURL);

    //Create an Explicit wait.
    WebDriverWait myWait2 = new WebDriverWait(driver, 10);
    myWait2.until(ExpectedConditions.visibilityOfElementLocated(By.id("persist_box")));

    WebElement chkFBPersist = driver.findElement(By.id("persist_box"));
    for (int i=0; i<2; i++) {
        chkFBPersist.click (); 
        System.out.println(chkFBPersist.isSelected());
    }

    driver.quit();
    System.exit(0);
}
}

1500950108846   geckodriver INFO    geckodriver 0.18.0
1500950108853   geckodriver INFO    Listening on 127.0.0.1:46140
1500950109519   geckodriver::marionette INFO    Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe with args ["-marionette"]
1500950111367   Marionette  INFO    Listening on port 56333
Jul 25, 2017 12:35:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: Register here
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'DESKTOP-OOA5BSF', ip: '101.011.01.111', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_141'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\Bain1\AppData\Local\Temp\rust_mozprofile.BQUc6ozUnkyX, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=54.0.1, platformVersion=10.0, moz:processID=15212.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
Session ID: b78462ce-e9bf-4143-a4a4-5c8b5a99f863
*** Element info: {Using=link text, value=Register here}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:461)
    at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
    at src.ha.main(ha.java:35)

1 个答案:

答案 0 :(得分:0)

以下是您的问题的答案:

要点击网址Register here中的http://newtours.demoaut.com/链接,您可以使用以下代码行:

// Click on "Register here"
driver.findElement(By.xpath("//a[contains(@href,'mercuryregister.php?')][contains(text(),'Register')]")).click();

如果这回答你的问题,请告诉我。