I encountered a problem selecting locators with dynamic properties. While I was able to select elements using their text values, I believe this method may not be reliable for automation testing.
Could you help me understand how to select the radio button labeled "One Way" when it has dynamic properties?
<div class="css-1dbjc4n" xpath="1">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n"></div>
<div class="css-1dbjc4n r-1d09ksm r-1inuy60 r-1qxgc49" style="padding-right: 0px; padding-left: 0px;">
<div class="css-1dbjc4n"></div>
<div class="css-1dbjc4n">
<div class="css-1dbjc4n r-18u37iz r-1w6e6rj">
<div class="css-1dbjc4n r-1awozwy r-1loqt21 r-18u37iz r-117bsoe r-1p4rafz r-1otgn73" data-focusable="true" tabindex="0" data-testid="one-way-radio-button" style="margin-right: 20px;">
<div class="css-1dbjc4n r-zso239">
<svg data-testid="svg-img" viewBox="0 0 18 18" width="18" height="18" fill="#000" color="#000" preserveAspectRatio="none">
<g fill="none" fill-rule="evenodd">
<circle cx="9" cy="9" r="8" fill="#FFF" stroke="#F7941D" stroke-width="2"></circle>
<circle cx="9" cy="9" r="4" fill="#EDB16A"></circle>
</g>
</svg>
</div>
<div class="css-1dbjc4n">
<div class="css-76zvg2 r-homxoj r-ubezar r-1ozqkpa" dir="auto" style="">one way</div>
</div>
</div>
<div class="css-1dbjc4n r-1awozwy r-1loqt21 r-18u37iz r-117bsoe r-1p4rafz r-1otgn73" data-focusable="true" tabindex="0" data-testid="round-trip-radio-button" style="margin-right: 20px;">
<div class="css-1dbjc4n r-zso239">
<svg data-testid="svg-img" viewBox="0 0 18 18" width="18" height="18" fill="#000" color="#000" preserveAspectRatio="none">
<circle cx="9" cy="9" r="8" fill="#FFF" fill-rule="evenodd" stroke="#DDDDDD" stroke-width="2"></circle>
</svg>
</div>
<div class="css-1dbjc4n">
<div class="css-76zvg2 r-homxoj r-ubezar r-1ozqkpa" dir="auto" style="font-family: inherit;">round trip</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I encountered a problem selecting locators with dynamic properties. While I was able to select elements using their text values, I believe this method may not be reliable for automation testing.
Could you help me understand how to select the radio button labeled "One Way" when it has dynamic properties?
<div class="css-1dbjc4n" xpath="1">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n"></div>
<div class="css-1dbjc4n r-1d09ksm r-1inuy60 r-1qxgc49" style="padding-right: 0px; padding-left: 0px;">
<div class="css-1dbjc4n"></div>
<div class="css-1dbjc4n">
<div class="css-1dbjc4n r-18u37iz r-1w6e6rj">
<div class="css-1dbjc4n r-1awozwy r-1loqt21 r-18u37iz r-117bsoe r-1p4rafz r-1otgn73" data-focusable="true" tabindex="0" data-testid="one-way-radio-button" style="margin-right: 20px;">
<div class="css-1dbjc4n r-zso239">
<svg data-testid="svg-img" viewBox="0 0 18 18" width="18" height="18" fill="#000" color="#000" preserveAspectRatio="none">
<g fill="none" fill-rule="evenodd">
<circle cx="9" cy="9" r="8" fill="#FFF" stroke="#F7941D" stroke-width="2"></circle>
<circle cx="9" cy="9" r="4" fill="#EDB16A"></circle>
</g>
</svg>
</div>
<div class="css-1dbjc4n">
<div class="css-76zvg2 r-homxoj r-ubezar r-1ozqkpa" dir="auto" style="">one way</div>
</div>
</div>
<div class="css-1dbjc4n r-1awozwy r-1loqt21 r-18u37iz r-117bsoe r-1p4rafz r-1otgn73" data-focusable="true" tabindex="0" data-testid="round-trip-radio-button" style="margin-right: 20px;">
<div class="css-1dbjc4n r-zso239">
<svg data-testid="svg-img" viewBox="0 0 18 18" width="18" height="18" fill="#000" color="#000" preserveAspectRatio="none">
<circle cx="9" cy="9" r="8" fill="#FFF" fill-rule="evenodd" stroke="#DDDDDD" stroke-width="2"></circle>
</svg>
</div>
<div class="css-1dbjc4n">
<div class="css-76zvg2 r-homxoj r-ubezar r-1ozqkpa" dir="auto" style="font-family: inherit;">round trip</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Share
Improve this question
edited Mar 31 at 2:58
JeffC
25.9k5 gold badges34 silver badges55 bronze badges
asked Mar 30 at 15:48
amalamal
3,60210 gold badges31 silver badges45 bronze badges
1
- Edit your question and post the code you are using as text, properly formatted. Then add the full error message you get when running that code, as text properly formatted. – JeffC Commented Mar 31 at 5:14
1 Answer
Reset to default 0You can use the below XPath expressions:
To locate One Way button:
//div[text()='one way']
or(//div[@class='css-76zvg2 r-homxoj r-ubezar r-1ozqkpa'])[1]
To locate Round Trip button:
//div[text()='round trip']
or(//div[@class='css-76zvg2 r-homxoj r-ubezar r-1ozqkpa'])[2]
Code for reference:
Below code launches the target URL --> clicks on Round Trip button --> waits 10s --> clicks on One Way button --> waits 10s --> terminates.
driver.get("https://www.spicejet/");
WebElement roundTrip = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@class='css-76zvg2 r-homxoj r-ubezar r-1ozqkpa'])[2]")));
roundTrip.click();
Thread.sleep(10000);
WebElement oneWay = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@class='css-76zvg2 r-homxoj r-ubezar r-1ozqkpa'])[1]")));
oneWay.click();
Thread.sleep(10000);
driver.quit();
Imports:
import .openqa.selenium.By;
import .openqa.selenium.WebDriver;
import .openqa.selenium.WebElement;
import .openqa.selenium.chrome.ChromeDriver;
import .openqa.selenium.support.ui.ExpectedConditions;
import .openqa.selenium.support.ui.WebDriverWait;