无法使用 selenium 单击 href link
Unable to click on href link using selenium
我无法使用下面的代码打开 href link。我已经使用代码将标签名称存储为 Web 元素并迭代以指向我的目标 href。请建议在上面的代码中更改什么,因为输出表明存在空引用。
String path="http://google.com";
WebDriver driver = new ChromeDriver();
driver.get(path);
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("hdmi");
driver.findElement(By.name("btnG")).click();
//first get all the <a> elements
List<WebElement> linkList=driver.findElements(By.tagName("a"));
//now traverse over the list and check
for(int i=0 ; i<linkList.size() ; i++)
{
if(linkList.get(i).getAttribute("href").contains("http://www.hdmi.org/"))
{
linkList.get(i).click();
break;
}
}
您需要执行一些 wait
才能找到如下列表:-
String path="http://google.com";
WebDriver driver = new ChromeDriver();
driver.get(path);
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("hdmi");
driver.findElement(By.name("btnG")).click();
//wait..
Thread.sleep(2000);
//first get all the <a> elements
List<WebElement> linkList = driver.findElements(By.tagName("a"));
//now traverse over the list and check
for(WebElement el : linkList)
{
String link = el.getAttribute("href");
if((link !=null) && (link.contains("http://www.hdmi.org/")))
{
el.click();
break;
}
}
为了获得更好的解决方案,您可以在此处使用 WebDriverWait
来查找 link,而无需使用如下循环:-
driver.findElement(By.name("q")).sendKeys("hdmi");
driver.findElement(By.name("btnG")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement link = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@href,'http://www.hdmi.org/')]")));
link.click();
希望对您有所帮助...:)
在这种情况下,您不必遍历 link。你可以找到你想要的那个然后点击它。加载结果时您必须稍等片刻,否则将无法运行。我猜这就是您的代码无法正常工作的原因。
driver.findElement(By.name("q")).sendKeys("hdmi");
driver.findElement(By.name("btnG")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href*='http://www.hdmi.org/']"))).click();
注意:有多个 link 符合您的要求,但此代码仅点击第一个。
我无法使用下面的代码打开 href link。我已经使用代码将标签名称存储为 Web 元素并迭代以指向我的目标 href。请建议在上面的代码中更改什么,因为输出表明存在空引用。
String path="http://google.com";
WebDriver driver = new ChromeDriver();
driver.get(path);
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("hdmi");
driver.findElement(By.name("btnG")).click();
//first get all the <a> elements
List<WebElement> linkList=driver.findElements(By.tagName("a"));
//now traverse over the list and check
for(int i=0 ; i<linkList.size() ; i++)
{
if(linkList.get(i).getAttribute("href").contains("http://www.hdmi.org/"))
{
linkList.get(i).click();
break;
}
}
您需要执行一些 wait
才能找到如下列表:-
String path="http://google.com";
WebDriver driver = new ChromeDriver();
driver.get(path);
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("hdmi");
driver.findElement(By.name("btnG")).click();
//wait..
Thread.sleep(2000);
//first get all the <a> elements
List<WebElement> linkList = driver.findElements(By.tagName("a"));
//now traverse over the list and check
for(WebElement el : linkList)
{
String link = el.getAttribute("href");
if((link !=null) && (link.contains("http://www.hdmi.org/")))
{
el.click();
break;
}
}
为了获得更好的解决方案,您可以在此处使用 WebDriverWait
来查找 link,而无需使用如下循环:-
driver.findElement(By.name("q")).sendKeys("hdmi");
driver.findElement(By.name("btnG")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement link = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@href,'http://www.hdmi.org/')]")));
link.click();
希望对您有所帮助...:)
在这种情况下,您不必遍历 link。你可以找到你想要的那个然后点击它。加载结果时您必须稍等片刻,否则将无法运行。我猜这就是您的代码无法正常工作的原因。
driver.findElement(By.name("q")).sendKeys("hdmi");
driver.findElement(By.name("btnG")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href*='http://www.hdmi.org/']"))).click();
注意:有多个 link 符合您的要求,但此代码仅点击第一个。