我正在尝试获取特定页面上存在的所有链接的链接名称,但是某些链接在将鼠标悬停在特定链接上后会显示,就像这些链接之间存在父子关系而Selenium WebDriver无法识别子链接的链接名称。
有没有办法获得包括父母和孩子在内的所有链接名称?
package test;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import mx4j.tools.remote.http.HTTPConnectionManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class SparshLink {
WebDriver driver;
@Before
public void setUp() throws Exception {
String basePath = System.getProperty("user.dir");
String finalPath = basePath + "\\IEDriver\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", finalPath);
driver = new InternetExplorerDriver();
driver.navigate().to("http://sparshv2/");
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void test() throws Exception {
List<WebElement> list;
list = driver.findElements(By.tagName("a"));
System.out.println("Number of links : " + list.size());
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
for (int i = 0; i < list.size(); i++) {
System.out.println(i);
URL url = new URL(list.get(i).getAttribute("href"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode() != 404) {
System.out.println(list.get(i).getText() + "is working properly.");
} else {
System.out.println(list.get(i).getText() + "is not working properly.");
}
connection.disconnect();
}
}
}
对不起,我无法分享应用程序背后的代码。
答案 0 :(得分:0)
'a'将成为标签,但我相信您的所有网址都应该在href属性中。
您应该参考以下内容: -
Finding all "A" tags with specific strings in the attribute href?