简体   繁体   English

无法单击硒列表Webelement中的第二个Webelement(anchor标签)

[英]Not able to click on the 2nd webelement(anchor tag) in a list webelement in selenium

Approach:- I am collecting all the IFrames and looping over them to find the particular xpath to click. 方法:-我正在收集所有IFrame,并在它们上循环以找到要单击的特定xpath。 if it found it will click on that element. 如果找到,它将单击该元素。

Problem:- while looping to a nested IFrame i am looking for an element by xpath but i am not able to identify the element uniquely, instead it identifies 3 elements. 问题:-当循环到嵌套的iframe时,我正在通过xpath查找元素,但是我无法唯一地标识该元素,而是标识了3个元素。 so i am putting it in a List..and when the xpath is identified in any frame i want to click on the 2nd link(anchor link) of the list webElement. 所以我把它放在一个列表中。当在任何框架中标识了xpath时,我想单击列表webElement的第二个链接(锚链接)。

But it is not clicking on the link and trowing error"Element not visible". 但是它不是单击链接并显示错误“元素不可见”。 below is the code i am using:- 以下是我正在使用的代码:-

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");
            }

here is the HTML:- 这是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>

I want to click on the 2nd tag link. 我想点击第二个标签链接。

Please help.. 请帮忙..

thanks 谢谢

You can identify the desired item by text using xpath 您可以使用xpath通过text识别所需的项目

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

You can also wait for the desired element until it's visible. 您也可以wait所需的元素,直到它可见。 If it's not visible, WebDriver will throw TimeoutException 如果它是不可见的,webdriver的将抛出TimeoutException

Your code should look like this: 您的代码应如下所示:

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();

Or if You don't want to find the element by its name , You can use your iteration approach like this: 或者,如果您不想按其名称查找元素 ,则可以使用以下迭代方法:

 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 POSSIBLE SOLUTION TO FIX VISIBILITY EXCEPTION: 编辑可能的解决方案以修复可见性异常:

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

Actions interface allows you to simulate mouse movements. Actions界面可让您模拟鼠标的移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM