简体   繁体   English

根据与单选按钮相邻的文本定位单选按钮的xpath

[英]Locating xpath of a radio button based on the text adjacent to it

<input id="lblProductName+Modem promo +default" 
    class="cmpAvailableSingleSelectProduct-radioButton" 
    type="radio" 
    onclick="cmpAvailableSSProductRadioToggleHandler(event, this,'Line_0.DSL.modemcre_s021403', 'false');" 
    name="defaultLine_0.DSL.modemcre_s021403Modem promo">

The above type is a radio button where the id is based on the next div tag. 上面的类型是一个单选按钮,其id基于下一个div标签。

<div class="cmpAvailableSingleSelectProduct-lblProductName">Modem promo - 100 </div>

I do know what will be the name (Modem promo - 100) inside the div class. 我确实知道div类中的名称(调制解调器促销-100)是什么。 But I'd like to click on the radio button adjacent to that. 但我想单击旁边的单选按钮。 I will be getting the value of "Modem promo - 100" as a string. 我将以字符串形式获取“调制解调器促销-100”的值。 I'd like to compare with the text and click on the adjacent radio button. 我想与文字进行比较,然后单击相邻的单选按钮。

Please help me on how to write xpath or finding the webElement. 请帮助我如何编写xpath或查找webElement。 Thanks! 谢谢!

You should try using xpath as below :- 您应该尝试使用xpath,如下所示:

String ValueOfPromo = "Modem promo - 100";

String xpath = "(.//div[contains(.,'" +ValueOfPromo+ "')]/preceding::input)[1]";

driver.findElement(By.xpath(xpath));

Hope it helps..:) 希望能帮助到你..:)

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

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