我可以使用selenium单击链接,但它会为我打开一个新选项卡,而不是在同一个选项卡中打开,它只显示数据;在地址栏中,而不是页面中的内容。
公共类InternetExplorerDriverTest { public static void clickLinkByHref(String href) {
WebDriver driver = new ChromeDriver();
List<WebElement> anchors = driver.findElements(By.tagName("a"));
Iterator<WebElement> i = anchors.iterator();
while(i.hasNext()) {
WebElement anchor = i.next();
if(anchor.getAttribute("href").contains(href)) {
anchor.click();
break;
}
}
}
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://url");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement element =
driver.findElement(By.id("323bb07bc611227a018aea9eb8f3b35e"));
element.click();
clickLinkByHref("ea7d96126f92a100f3ae60526e3ee4e0");
}
}
日食中的控制台在2分钟后显示在下面&#39;
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.1.7601 SP1 x86) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 61.87 seconds
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'NLLR4000290859', ip: '10.29.137.219', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_31'
Driver info: org.openqa.selenium.chrome.ChromeDriver
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:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)
at org.openqa.selenium.chrome.ChromeDriver.startSession(ChromeDriver.java:182)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:111)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:115)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:161)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:150)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:107)
at InternetExplorerDriverTest.clickLinkByHref(InternetExplorerDriverTest.java:41)
at InternetExplorerDriverTest.main(InternetExplorerDriverTest.java:94)
答案 0 :(得分:0)
尝试查看此示例:https://sites.google.com/a/chromium.org/chromedriver/getting-started
此外,您的链接是否可能定位 _blank ?
这是一个有效的简单示例:
package org.mitre.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Wtest {
public static void main(String[] args) {
try {
System.setProperty("webdriver.chrome.driver", "exe\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/");
Thread.sleep(500);
WebElement text_link = driver.findElement(By.linkText("Learn HTML"));
text_link.click();
Thread.sleep(5000);
driver.quit();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("done.");
} //end main()
}
答案 1 :(得分:0)
我在改述我的问题:前一个问题是因为我正在重新初始化chromedriver。 所以这次我将驱动程序对象传递给方法。
现在我发现我尝试点击的链接根本没有被识别出来。 chrome中的开发人员工具向我显示了以下结构中的链接:
anchors = dd.findElements(By.linkText(Standard (from Template)));
anchors2 = dd.findElements(By.cssSelector(a[href*='ea7d96126f92a100f3ae60526e3ee4e0'] )));
如何识别并单击此特定文本链接?我尝试过css选择器,xpath等但是没有工作
(:use [clojure.java.shell :only [sh]])
(sh "ls" "-aul"); <-- WORKS
=>
{:exit 0,
:out "total 136
-rw-r--r-- 1 snaggs staff 113 Jan 8 14:17 .babelrc
drwxr-xr-x 15 snaggs staff 510 Jan 12 14:14 .hg
此外,还有10个具有相同类名的div部分,每个部分都是文本链接。我想点击它们(见上文)
答案 2 :(得分:0)
driver.switchTo().frame(driver.findElement(By.id("gsft_main")));
上述代码行需要切换到iframe。这现在有效。