package Com.Automation;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Second_Findelements {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.get("http://www.google.com");
System.out.println(driver.getCurrentUrl());
System.out.println(driver.getTitle());
driver.findElement(By.name("q")).sendKeys("gmail");
driver.findElement(By.xpath("//a[(text()='Gmail - Google']")).click();
driver.quit();
}
}
错误是:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException:
The given selector //a[(text()='Gmail - Google'] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression //a[(text()='Gmail - Google'] because of the following error:
SyntaxError: The expression is not a legal expression.
Command duration or timeout: 30 milliseconds
For documentation on this error, please visit:
http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'shaheena-PC', ip: '100.68.111.156', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=43.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 62a7b699-fabb-4742-82dc-6f7785f33275
*** Element info: {Using=xpath, value=//a[(text()='Gmail - Google']}
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.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:490)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:345)
at Com.Automation.Second_Findelements.main(Second_Findelements.java:23)
Caused by: org.openqa.selenium.InvalidSelectorException: The given selector //a[(text()='Gmail - Google'] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression //a[(text()='Gmail - Google'] because of the following error:
SyntaxError: The expression is not a legal expression.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'shaheena-PC', ip: '100.68.111.156', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.annotateInvalidSelectorError_(file:///C:/Users/shaheena/AppData/Local/Temp/anonymous3401694128124177427webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10633)
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/shaheena/AppData/Local/Temp/anonymous3401694128124177427webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10664)
at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/shaheena/AppData/Local/Temp/anonymous3401694128124177427webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10668)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/shaheena/AppData/Local/Temp/anonymous3401694128124177427webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/shaheena/AppData/Local/Temp/anonymous3401694128124177427webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/shaheena/AppData/Local/Temp/anonymous3401694128124177427webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
答案 0 :(得分:1)
您写错了xpath:
//a[(text()='Gmail - Google']
真正的xpath是:
//a[text()='Gmail - Google']
您已添加额外&#34;(&#34;
如果你想使用contains
//a[contains(text(),'Gmail')]
在上面的xpath
中使用唯一文字而不是Gmail