无法单击selenium中列表webelement中的第二个webelement(锚标记)

时间:2017-12-01 14:40:26

标签: java html xpath selenium-webdriver

方法: - 我正在收集所有IFrame并循环遍历它们以找到要单击的特定xpath。 如果发现它会点击该元素。

问题: - 在循环到嵌套的IFrame时,我正在寻找xpath元素,但我无法唯一地识别元素,而是识别出3个元素。 所以我把它放在List ..当在任何帧中识别xpath时,我想点击列表webElement的第二个链接(锚链接)。

但它没有点击链接和投掷错误“元素不可见”。 下面是我正在使用的代码: -

String xPath = ".//a[@data-seclkr='1']";//xpath of 3 identified webelement
 Boolean iselementpresent = sfdc.GetWebDriver().findElements(By.xpath(xPath)).size()!= 0;
            if (iselementpresent==true)
            {
                System.out.println("Yes element is present in nested for case Link");
                List<WebElement> casedetails = sfdc.GetWebDriver().findElements(By.xpath(".//a[@data-seclkr='1']"));
                casedetails.get(1).click();
break;
            }
            else{
                System.out.println("No element is NOT  present in nested for case Link");
            }

这是HTML: -

<td class="actionColumn">
<a class="actionLink" title="Edit - Record 1 - Test FEA case" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/5000v0000018Jgj/e" data-seclki="5000v0000018Jgj" data-seclkh="294f7f9698757809b8e22aba88fe090c" data-seclke="Case" href="javascript:srcUp(%27%2F5000v0000018Jgj%2Fe%3FsrPos%3D0%26srKp%3D500%26retURL%3D%252F_ui%252Fsearch%252Fui%252FUnifiedSearchResults%253FsearchType%253D2%2526sen%253D500%2526sen%253Da6L%2526sen%253Da6M%2526str%253D%252509%252B02309343*%2526isdtp%253Dvw%2526isWsVw%253Dtrue%2526isWsVw%253Dtrue%2526isWsVw%253Dtrue%2526nonce%253D1e0c5b181417fcc9ba9f5c87794ccaf8671265f86f9f82afbaa70ccb35599c53%2526sfdcIFrameOrigin%253Dhttps%25253A%25252F%25252Fmetlifeusdirectsales--ebsdev.cs66.my.salesforce.com%26isdtp%3Dvw%27);">Edit</a>
</td>
<th class=" dataCell cellCol1 " scope="row">
<a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/5000v0000018Jgj" data-seclki="5000v0000018Jgj" data-seclkh="294f7f9698757809b8e22aba88fe090c" data-seclke="Case" href="javascript:srcUp(%27%2F5000v0000018Jgj%3FsrPos%3D0%26srKp%3D500%26isdtp%3Dvw%27);">02309343</a>
</th>
<td class=" dataCell cellCol2 "/>
<td class=" dataCell cellCol3 "/>
<td class=" dataCell cellCol4 ">
<a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/5000v0000018Jgj" data-seclki="5000v0000018Jgj" data-seclkh="294f7f9698757809b8e22aba88fe090c" data-seclke="Case" href="javascript:srcUp(%27%2F5000v0000018Jgj%3FsrPos%3D0%26srKp%3D500%26isdtp%3Dvw%27);">Test FEA case</a>
</td>

我想点击第二个标记链接。

请帮助..

感谢

1 个答案:

答案 0 :(得分:0)

您可以使用text

xpath之前识别所需的项目

//a[contains(text(), '02309343')]

您还可以wait获取所需元素,直到它可见为止。如果它不可见,WebDriver将抛出TimeoutException

您的代码应如下所示:

By locator = By.xpath("//a[contains(text(), '02309343')]");
WebDriverWait wait = new WebDriverWait(sfdc.GetWebDriver(), 10)
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
sfdc.GetWebDriver().findElement(locator).click();

或者,如果您不想按名称找到该元素,您可以使用以下迭代方法:

 String xPath = ".//a[@data-seclkr='1']";//xpath of 3 identified webelement
 if (sfdc.GetWebDriver().findElements(By.xpath(xPath)).size()!= 0) {
     System.out.println("Yes element is present in nested for case Link");
     List<WebElement> casedetails = sfdc.GetWebDriver().findElements(xPath);
     WebDriverWait wait = new WebDriverWait(sfdc.GetWebDriver(), 10)
     wait.until(ExpectedConditions.visibilityOf(casedetails.get(1)));
     casedetails.get(1).click();
 } else {
     System.out.println("No element is NOT  present in nested for case Link");
 }

EDIT 固定可见性的可能解决方案:

WebElement myElement = driver.findElement(By.xpath("//th[@scope='row']/a"));
new Actions(driver)
                .moveToElement(myElement)
                .click()
                .perform();

Actions界面允许您模拟鼠标移动。