我是硒的新手。我在谷歌搜索我的名字,然后从结果我想选择一个链接。 代码是:
public class linkedin {
public static void main(String[] args)
{
ChromeOptions opt= new ChromeOptions();
System.setProperty("webdriver.chrome.driver","path");
WebDriver d = new ChromeDriver(opt);
opt.addArguments("--start-maximized");
d.get("http://www.google.com");
String title=d.getTitle();
System.out.println(title);
WebElement myDynamicElement = (new WebDriverWait(d, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("gs_htif0")));
WebElement btnNew = (new WebDriverWait(d, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("_fZl")));
//WebElement searc = (new WebDriverWait(d, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[contains(text(),'Neethu Raghavan | LinkedIn')]")));
WebElement searc=(new WebDriverWait(d,10).until(ExpectedConditions.elementToBeClickable(By.linkText("Neethu Raghavan | LinkedIn"))));
Actions act= new Actions(d);
act.moveToElement(myDynamicElement).sendKeys("Neethu Raghavan");
act.perform();
act.moveToElement(btnNew).click();
act.perform();
act.moveToElement(searc).click();
act.perform();
}
}
直到搜索它运作良好,结果来了。但在此之后没有任何事情发生。
我得到的错误是:
Exception in thread "main" org.openqa.selenium.TimeoutException:
Expected condition failed: waiting for element to be clickable: By.linkText: Neethu Raghavan | LinkedIn (tried for 10 second(s) with 500 MILLISECONDS interval)
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'INENRAGHAN1L1C', ip: '10.171.40.246', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_91'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9), userDataDir=C:\Users\raghan1\AppData\Local\Temp\scoped_dir19884_11542}, 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=}]
Session ID: 9e89b95716b724c5d5e06751ce1aaf5d
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:265)
at linkedin.main(linkedin.java:31)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Neethu Raghavan | LinkedIn"}
(Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.10586 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 46 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'INENRAGHAN1L1C', ip: '10.171.40.246', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_91'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9), userDataDir=C:\Users\raghan1\AppData\Local\Temp\scoped_dir19884_11542}, 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=}]
Session ID: 9e89b95716b724c5d5e06751ce1aaf5d
*** Element info: {Using=link text, value=Neethu Raghavan | LinkedIn}
at sun.reflect.GeneratedConstructorAccessor8.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:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:425)
at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:895)
at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:41)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:651)
at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:644)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238)
答案 0 :(得分:1)
@Neethu尝试此代码。
driver.get("http://google.com");
driver.manage().window().maximize();
WebElement name = driver.findElement(By.id("lst-ib"));
name.sendKeys("Neethu Raghavan");
driver.findElement(By.id("_fZl")).click();
Thread.sleep(2500); // wait for 2.5 seconds
执行上述代码后,您可以选择要点击的任何网站。
在您的情况下,从谷歌搜索您的名字后显示列出的网站数量。比如网站名称为“facebook”“linkedin”等。
假设您要点击linkedin
网站。所以使用xpath定位器找到webelement。
所以你的其余代码看起来就像下面的代码片段。
driver.findElement(By.xpath("//a[@href='https://in.linkedin.com/in/neethu-raghavan-8312a839']")).click();