java - selenium - 我可以获取元素的文本,但不能点击它的链接 [英] java - selenium - I can get text of element but can't click its link

查看:704
本文介绍了java - selenium - 我可以获取元素的文本,但不能点击它的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的关于该问题的代码片段:

  WebElement option = driver.findElement(By.id(AccNumSpan+ i)); 
if(option.getText()。contains(48494)){
List< WebElement> listOfOptions1 = driver.findElements(By.xpath(// * [@ id ='row+ i +''] / td [10] / nobr));
for(WebElement option1:listOfOptions1){
System.out.println(option1.getText());

String id = option1.getAttribute(id);
System.out.println(id);
option1.click();
}

以下是我试图点击的链接的html: p>

 < td class =cellalign =left> 
< nobr>
< span id =link15796367class =stmtListTableLinkDocumentonclick =billDocsClient.ShowDocTypeList(event,'table#StatementTable','span#link15796367',908244,15796367,'6728_22198','48494(Merrill Lynch Europe PLC)','6/1/2014','34969691/175474'); onmouseout =this.className ='stmtListTableLinkDocument'onmouseover =this.className ='docLink'>查看每月发票< / span>
< / nobr>
< / td>

当我运行代码时,它从option1.getText输出View Monthly Statement找到元素),但是当我尝试option1.click()(这是我想要做的)除了文本被强调或什么都没有发生,但链接没有打开。另外,它不会打印任何option1.getAttribute(id)或任何使用.getAttribute的属性。



有什么办法可以解决这个问题?所有帮助表示赞赏,谢谢 尝试点击您的nobr下的span元素:

  option1.findElement(By.xpath(span))。click(); 

,因为这似乎是可点击的元素。


I am having trouble clicking on a link in a webpage using the selenium webdriver in firefox.

Here is a snippet of my code regarding the issue:

WebElement option = driver.findElement(By.id("AccNumSpan" + i));
                if(option.getText().contains("48494")){ 
                    List <WebElement> listOfOptions1 = driver.findElements(By.xpath("//*[@id='row" + i +"']/td[10]/nobr"));
                    for(WebElement option1 : listOfOptions1){
                        System.out.println(option1.getText());

                        String id = option1.getAttribute("id");
                        System.out.println(id);
                        option1.click();
}

Here is the html of the link that I am trying to click:

<td class="cell" align="left">
<nobr>
<span id="link15796367" class="stmtListTableLinkDocument" onclick="billDocsClient.ShowDocTypeList(event, 'table#StatementTable', 'span#link15796367', 908244, 15796367, '6728_22198', '48494 (Merrill Lynch Europe PLC)', '6/1/2014', '34969691/175474');" onmouseout="this.className='stmtListTableLinkDocument'" onmouseover="this.className='docLink'">View Monthly Invoices</span>
</nobr>
</td>

When I run the code, it prints "View Monthly Statement" from option1.getText ( so it does find the element), but when I try option1.click() (which is what I want to do) nothing happens except the text gets underlined or whatever but the link isn't opened. Also, it does not print anything for option1.getAttribute("id") or for any attribute using .getAttribute

Any way to get around this? All help is appreciated, thanks

解决方案

Try clicking on the span element below your nobr:

option1.findElement(By.xpath("span")).click();

as that seems to be the clickable element.

这篇关于java - selenium - 我可以获取元素的文本,但不能点击它的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆