如何在Google Chrome中选择第二个标签页中的第二个链接 浏览器
package Testing;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestPage {
WebDriver driver;
By text = By.linkText("http://www.phptravels.net");
By flights = By.xpath("//a[text()='Flights')]/@herf");
@BeforeTest
public void setup() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://phptravels.com/demo/");
driver.findElement(text).click();
}
@Test
public void flights() {
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(flights).click();
}
}
由于以下错误,FAILED: flights org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression
// a [text()='Flights')] / @ herf: SyntaxError:无法对'Document'执行'evaluate':字符串'// a [text()='Flights')] / @ herf'不是有效的XPath表达式。 (会议信息:chrome = 55.0.2883.87) (驱动程序信息:chromedriver = 2.27.440174(e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform = Windows NT 10.0.14393 x86)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:616毫秒 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/invalid_selector_exception.html 构建信息:版本:'未知',修订版:'1969d75',时间:'2016-10-18 09:43:45 -0700' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能[{applicationCacheEnabled = false,rotate = false,mobileEmulationEnabled = false,networkConnectionEnabled = false, 铬= {chromedriverVersion = 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9), userDataDir = C:\用户\ SOWJAN〜1 \应用程序数据\本地\ TEMP \ scoped_dir4704_3077}, takesHeapSnapshot = true,pageLoadStrategy = normal, databaseEnabled = false,handlesAlerts = true,hasTouchScreen = false, version = 55.0.2883.87,platform = XP,browserConnectionEnabled = false, nativeEvents = true,acceptSslCerts = true,locationContextEnabled = true, webStorageEnabled = true,browserName = chrome,takesScreenshot = true, javascriptEnabled = true,cssSelectorsEnabled = true, unexpectedAlertBehaviour =}] 会议ID:30e01d83efbd6b2eecc377a1222de35e ***元素信息:{Using = xpath,value = // a [text()='Flights')] / @ herf} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown 资源) at java.lang.reflect.Constructor.newInstance(Unknown Source) 在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216) 在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168) 在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635) 在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368) 在org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473) 在org.openqa.selenium.By $ ByXPath.findElement(By.java:361) 在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360) 在Testing.TestPage.flights(TestPage.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) at org.testng.internal.Invoker.invokeMethod(Invoker.java:645) 在org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851) 在org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) 在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 在org.testng.TestRunner.privateRun(TestRunner.java:756) 在org.testng.TestRunner.run(TestRunner.java:610) 在org.testng.SuiteRunner.runTest(SuiteRunner.java:387) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) 在org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) 在org.testng.SuiteRunner.run(SuiteRunner.java:289) 在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) at org.testng.TestNG.runSuitesLocally(TestNG.java:1218) 在org.testng.TestNG.runSuites(TestNG.java:1133) 在org.testng.TestNG.run(TestNG.java:1104) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
答案 0 :(得分:0)
XPath
中的selenium
表达式应该用于选择网络元素,而XPath
显然不正确,因为它指向属性(@href
中也存在拼写错误......还有一个额外的支架。你应该尝试以下:
driver.findElement(By.xpath("//a[normalize-space(.)='Flights']")).click();
答案 1 :(得分:0)
面对InvalidSelectorException
,因为您的xpath
无效,因为您使用@href
属性和额外的'}'支架
By flights = By.xpath("//a[text()='Flights')]/@herf"); // Your xpath
只需更改您的xpath,然后尝试 -
By flights = By.xpath("//a[contains(., 'Flights')]");