Java 如何读取页面上的所有标题标记并选择一个具有特定标题的hading标记?

Java 如何读取页面上的所有标题标记并选择一个具有特定标题的hading标记?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我正在尝试读取页面上的所有标题标记,只需单击一个名为“dropdown”的标题标记。HTML的示例结构如下所示 <div> <ul> <li> <a href="submit_button_clicked.php"> <h2>Submit Button Clicked</h2> <figure> </a> </li> <li> <a href="dropdown.php"

我正在尝试读取页面上的所有标题标记,只需单击一个名为“dropdown”的标题标记。HTML的示例结构如下所示

<div> <ul> <li>
<a href="submit_button_clicked.php">
<h2>Submit Button Clicked</h2>
<figure>
</a>
</li>
<li>
<a href="dropdown.php">
<h2>Dropdown</h2>
<figure>

要检索元素的文本值,请使用:

element.getText();
在您的列表中,它将如下所示:

for(WebElement element : l) {
    System.out.println(element.getText());
}
由于要单击元素,因此最好使用xpath,如以下所示:

ff.findElements(By.xpath("h2[text()='Dropdown']")).click();
查找并单击所需的特定图元。上面的xpath选择器查找具有确切文本“Dropdown”的h2元素,然后单击它。

读取所有
标记可能类似于:

List<WebElement> elements = ff.findElements(By.xpath("//h2"));
for(WebElement element : elements) {
    System.out.println(element.getText()); // just to show that it prints text
}
但如果您想从标题中查找链接,请在上面的循环中:

List<WebElement> elements = ff.findElements(By.xpath("//h2"));
for(WebElement element : elements) {
    if("Download".equals(element.getText()) {
        // get the parent <a> element and click on it
        element.findElement(By.xpath("..")).click();
    }
}
List elements=ff.findElements(By.xpath(“//h2”);
for(WebElement:elements){
if(“Download”.equals(element.getText()){
//获取父元素并单击它
element.findElement(By.xpath(“..”).click();
}
}

您好,请按以下方式操作

WebDriver driver = new FirefoxDriver();
driver.get("http://www.seleniumhq.org");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Hi please do it like below ,take all H2 tag inside the list

List<WebElement> myH2Tags = driver.findElements(By.tagName("h2"));  // you can put any tag name as per your requirement

for(int i=0;i<myH2Tags.size();i++){
    System.out.println("Value of My H2 Tags are : " + myH2Tags.get(i).getText());
    if(myH2Tags.get(i).getText().equals("Selenium News")){ // you can replace this with drop down value
    myH2Tags.get(i).click();
        }
// to avoid stale element exception you have to re identify the elements
            myH2Tags = driver.findElements(By.tagName("h2"));
        }
WebDriver=newfirefoxdriver();
驱动程序。获取(“http://www.seleniumhq.org");
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
//您好,请按下面的方式操作,将列表中的所有H2标记
List myH2Tags=driver.findElements(按.tagName(“h2”);//您可以根据需要放置任何标记名

对于(int i=0;i在遍历元素时,我遇到了陈旧的元素异常,因此无法打印所有值。欢迎使用任何解决方案:)您看到了吗:底线是:要么您获取这些元素太早,而页面仍在更改/加载;要么页面一直在更改。因此您有几个选项:1-在获取元素之前等待更长时间;2-捕获异常并继续使用列表中的其他元素;3-从头重试。g的最终目标是什么设置所有
元素?最终目标:使脚本更具动态性。只需单击所有类似标题中的一个标题。但为什么要在所有标题上循环?通过在xpath中提供足够的信息,可以直接获得所需的标题,获取其父链接并单击它,如下所示:
WebElement dropdown=ff.findElement(By.xpath(“//h2[text()='Dropdown']));Dropdown.findElement(By.xpath(“…”)).click();}
就是这样。在xpath中提供足够的信息,而不需要迭代所有标题标记。这对我很有用。当我直接使用上述代码By.xpath(“h2[text()='Dropdown']))时,单击();我得到一个例外“Webdriver元素不可单击”您的元素在尝试单击时由于某种原因不可单击。该下拉列表可能是禁用的元素,或者该元素的高度/宽度为零,或者该元素不可见。
List<WebElement> elements = ff.findElements(By.xpath("//h2"));
for(WebElement element : elements) {
    if("Download".equals(element.getText()) {
        // get the parent <a> element and click on it
        element.findElement(By.xpath("..")).click();
    }
}
WebDriver driver = new FirefoxDriver();
driver.get("http://www.seleniumhq.org");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Hi please do it like below ,take all H2 tag inside the list

List<WebElement> myH2Tags = driver.findElements(By.tagName("h2"));  // you can put any tag name as per your requirement

for(int i=0;i<myH2Tags.size();i++){
    System.out.println("Value of My H2 Tags are : " + myH2Tags.get(i).getText());
    if(myH2Tags.get(i).getText().equals("Selenium News")){ // you can replace this with drop down value
    myH2Tags.get(i).click();
        }
// to avoid stale element exception you have to re identify the elements
            myH2Tags = driver.findElements(By.tagName("h2"));
        }