我正在编码以标签形式打开链接,并打印所有链接标题;当我使用键然后使用窗口概念时,它应该打开选项卡上的链接,然后可以打印每个链接的页面标题。但是,在chrome中,它引发了过时的元素引用异常,而在Firefox中,它甚至没有产生任何制表符。 如果我在某处做错了,请查看以下代码并纠正我。谢谢。
package com.practice.one;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ClickingAllTheLinks {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"/Users/owner/desktop/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.qaclickacademy.com/practice.php");
List <WebElement> links=driver.findElements(By.tagName("a"));
for (int i=0;i<links.size();i++) {
System.out.println(links.get(i).getText());
}
WebElement footer_links= driver.findElement(By.cssSelector("div#gf-
BIG"));
List<WebElement>sub_links=footer_links.findElements(By.tagName("a"));
for (int i=0;i<sub_links.size();i++) {
System.out.println(sub_links.get(i).getText());
}
WebElement
left_footer_Links=driver.findElement(By.xpath("//table[@class='gf-
t']/tbody/tr/td[1]/ul"));
List<WebElement>leftfoot_Links=left_footer_Links.findElements
(By.tagName("a"));
for (int i=0;i<leftfoot_Links.size();i++) {
String clickandopenlinksonTabs=Keys.chord(Keys.CONTROL,
Keys.RETURN);
leftfoot_Links.get(i).sendKeys(clickandopenlinksonTabs);
Thread.sleep(3000L);
Set<String> wins=driver.getWindowHandles();
Iterator<String> it=wins.iterator();
while(it.hasNext()) {
driver.switchTo().window(it.next());
System.out.println(driver.getTitle());
}
}
}
}