Selenium WebDriver Java - 按标签单击元素不适用于某些标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23704518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium WebDriver Java - Clicking on element by label not working on certain labels
提问by qaRookie
I am trying to click on element by the label. Here is the code I am using:
我正在尝试通过标签单击元素。这是我正在使用的代码:
driver.findElement(By.xpath("id(//label[text() = 'LABEL TEXT HERE']/@for)")).click();
It works for (Select All) & Hayward but cant find Los Angeles, San Fran, or San Jose.
它适用于(全选)和海沃德,但找不到洛杉矶、旧金山或圣何塞。
UPDATE:
更新:
For now I guess this may be my best option until I see something better. This will allow the user to pass the full String and the function in the method will grab the last word of the string and insert it into the contains xpath.
现在我想这可能是我最好的选择,直到我看到更好的东西。这将允许用户传递完整的字符串,并且该方法中的函数将获取字符串的最后一个单词并将其插入 contains xpath。
public void subStringLocationTest(String location) {
String par = location.substring(location.lastIndexOf(" ") + 1);
driver.findElement(By.xpath("//label[contains(text(), '" + par + "')]")).click();
}
Here is the HTML code:
这是 HTML 代码:
<div id="ReportViewer1_ctl04_ctl03_divDropDown" onclick="event.cancelBubble=true;" onactivate="event.cancelBubble=true;" style="border: 1px solid rgb(169, 169, 169); font-family: Verdana; font-size: 8pt; overflow: auto; background-color: window; display: inline; position: absolute; z-index: 11; left: 131px; top: 41px; width: 188px;">
<span><table cellpadding="0" cellspacing="0" style="background-color:window;">
<tbody><tr>
<td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl00" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl00" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnSelectAllClick(this);"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl00">(Select All)</label></span></td>
</tr><tr>
<td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl02" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl02" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl02">Hayward</label></span></td>
</tr><tr>
<td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl03" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl03" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl03">Los Angeles</label></span></td>
</tr><tr>
<td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl04" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl04" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl04">San Francisco</label></span></td>
</tr><tr>
<td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl05" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl05" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl05">San Jose</label></span></td>
</tr>
</tbody></table><input type="hidden" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl01$HiddenIndices" id="ReportViewer1_ctl04_ctl03_divDropDown_ctl01_HiddenIndices" value=""></span>
</div>
回答by Sitam Jana
Use following xpaths :
使用以下 xpath:
//label[text()='(Select All)']
//label[text()='Hayward']
//label[text()='Los Angeles']
//label[text()='San Francisco']
//label[text()='San Jose']
回答by SirFrederick
You can also do a contains like this:
你也可以像这样做一个包含:
"//label[contains(text(), 'TEXT_TO_FIND')]"
回答by Abhishek Yadav
Try to use id instead of xpath:
尝试使用 id 而不是 xpath:
//for Select All
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl00")).click();
//for Hayward
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl02")).click();
//for Los Angeles
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl03")).click();
//for San Francisco
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl04")).click();
//for San Jose
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl05")).click();
If still have problem or id is changing dynamically you cane use xpath like:
如果仍然有问题或 id 正在动态更改,您可以使用 xpath 如下:
//label[contains(.,'(Select All)')]
//label[contains(.,'Hayward')]
//label[contains(.,'Los Angeles')]
//label[contains(.,'San Francisco')]
//label[contains(.,'San Jose')]
回答by stan
driver.findElement(By.xpath("//label[text()='LABEL TEXT']/../input")).click();
回答by Bandeesh R Sirga
Try CSS:driver.findElement(By.CSS("label:contains('Partial or full text')");
试试 CSS:driver.findElement(By.CSS("label:contains('Partial or full text')");
回答by Milky
try this, maybe good startpoint:
试试这个,也许是一个很好的起点:
System.out.print(driver.findElement(By.xpath("//div[@id='ReportViewer1_ctl04_ctl03_divDropDown']/span/table/tbody/tr[" + i +"]/td/span/label")).getText());
driver.findElement(By.xpath("//div[@id='ReportViewer1_ctl04_ctl03_divDropDown']/span/table/tbody/tr[" + i +"]/td/span/label")).click();
i = 3; //Los Angeles
i = 4; //San Francisco
I hope, my xpath is correct.
我希望,我的 xpath 是正确的。
回答by Ksingh
Use below code to click on label or choose label
使用下面的代码点击标签或选择标签
driver.findElement(By.xpath("//label[@for='your id which you want to click']")).click();
回答by Saurabh
You can use xpath locater. For example:
您可以使用 xpath 定位器。例如:
d.findElement(By.xpath("//...../label[contains(@for,'ReportViewer1_ctl04_ctl03_divDropDown_ctl02')]")).click();