简体   繁体   中英

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.

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

I do know what will be the name (Modem promo - 100) inside the div class. 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. 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. Thanks!

You should try using xpath as below :-

String ValueOfPromo = "Modem promo - 100";

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

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

Hope it helps..:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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