簡體   English   中英

在html上,表格形式的數據,我如何使用Selenium Web-driver Java單擊並在第二位置打印數據

[英]On html, data in table form, how i click and print data on 2nd position using selenium web-driver java

我如何單擊鏈接並使用Selenium WebDriver打印數據。 在這里,我想單擊鏈接Yahoo.com,並要打印“ Yahoo”,也要在“ Yahoo的搜索數據”中打印數據。這里,鏈接和可打印數據以及鏈接是動態的。 那么我如何使用Java語言中的Selenium Webdriver來完成這些操作

<table>
       <tbody>
                <tr>
                    <td>
                        <span>
                            <a href="www.google.com">Google</a>
                        </span>
                    </td>
                      <td>
                         <span>Searched data on Google</span>
                      </td>
                </tr>
               <tr>
                    <td>
                        <span>
                            <a href="www.yahoo.com">Yahoo</a>
                        </span>
                    </td>
                      <td>
                         <span>Searched data on Yahoo</span>
                        </td>
                </tr>
              </tr>
            </tbody>
        </table>

試用以下代碼:

void linkToClick(str linkName) {
    WebElement link = driver.findElement(By.xpath("//table//a[contains(.,'" + linkName + "')]")); // finding link with desired name like yahoo or google
    WebElement data = driver.findElement(By.xpath("//table//a[contains(.,'" + linkName + "')]/ancestor::td/following-sibling::td/span")); // finding text in the span next to provided link
    System.out.println(data.getText()); // printing text in span
    link.click(); // clicking link
}

希望這是你在尋找...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM