I need please your help to build XPath, click on the button :
HTML is :
<div class="offer col-md gtm-data gtm-impression slick-slide" data-gtm-id="5608" data-gtm-title="230 גיגה Rolling Package" data-gtm-price="33.00" data-gtm-category="חבילות" data-gtm-list="homepage" data-gtm-position="4" data-slick-index="3" aria-hidden="true" tabindex="-1" style="width: 370px;" xpath="1">
<div class="upper">
<div class="title"><spam class="threshold">230</spam><spam class="units">GB</spam></div>
<div class="subtitle"><p>Rolling Package</p>
</div>
<!--<div ><span>test</span></div>-->
</div>
<div class="bottom">
<div class="price-area">
<div class="title"><spam class="price-number"><span class="number">33</span></spam> </div>
<div class="subtitle">
<span></span>
</div>
</div>
<div class="link">
<a href="en/userGuide/step1?packageidReg=5608&packageidChange=5609&process=CustomerGuide" class="button red full gtm-click" title="Join Now" tabindex="-1" style="">Join Now</a>
</div>
</div>
</div>
When I try to click on the button, by XPath :
//div[contains(@data-gtm-id, '5608')] //a[@class='button red full gtm-click']
I got this error :
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <a href="userGuide/step1?packageidReg=5608&packageidChange=5609&process=CustomerGuide" class="button red full gtm-click" title="..." tabindex="0">להצטרפות</a> is not clickable at point (951, 858). Other element would receive the click: <p>...</p>
(Session info: chrome=96.0.4664.45)
When I try :
(//a[contains(@class,'button red')])[2]
I can click on the button, But I want the code to be more dynamic.
CodePudding user response:
The data-gtm-id
attribute is a Google Tag Manager attribute and the value of data-gtm-id attribute i.e. 5608 is dynamically generated. Everytime you access the application it would get changed. So you won't be able to locate the element using the attribute data-gtm-id
To click() on the element with text as Join Now you can use either of the following Locator Strategies:
linkText:
driver.findElement(By.linkText("Join Now")).click();
cssSelector:
driver.findElement(By.cssSelector("a.button.red.full.gtm-click[title='Join Now']")).click();
xpath:
driver.findElement(By.xpath("//a[@class='button red full gtm-click' and text()='Join Now']")).click();
However, as the element is a dynamic element so to click()
on the element you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Google"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.button.red.full.gtm-click[title='Join Now']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='button red full gtm-click' and text()='Join Now']"))).click();
CodePudding user response:
I hope your element is right but it is clicking before it is loaded, so please add wait condition.
By joinNowBtn = By.xpath("//a[contains(text(),'Join Now')]");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(joinNowBtn));
wait.until(ExpectedConditions.visibilityOfElementLocated(joinNowBtn));
driver.findElement(joinNowBtn).click();
CodePudding user response:
Thank you @Jayanth Bala, But I have in the code, several times the option of clicking on some "Join Now": enter image description here
How can I make it dependent on TAG - "data-gtm-id",
My code is :
<div class="slick-list draggable"><div class="slick-track" style="opacity: 1; width: 2340px; transform: translate3d(0px, 0px, 0px);"><div class="offer col-md gtm-data slick-slide slick-current slick-active" data-gtm-id="5663" data-gtm-title="1500 גיגה 476 2nd" data-gtm-price="54.00" data-gtm-category="חבילות" data-gtm-list="homepage" data-gtm-position="1" data-slick-index="0" aria-hidden="false" tabindex="0" style="width: 370px;">
<div class="upper">
<div class="title"><spam class="offer-content"><p>1500GB</p>
<spam></spam></spam></div>
<div class="subtitle"><p>476 2nd</p>
</div>
<!--<div ><span>test</span></div>-->
</div>
<div class="bottom">
<div class="price-area">
<div class="title"><spam class="price-number"><span class="number">54</span></spam> </div>
<div class="subtitle">
<span><p>476 </p>
</span>
</div>
</div>
<div class="link">
<a href="en/userGuide/step1?packageidReg=5663&packageidChange=5662&process=CustomerGuide" class="button red full gtm-click" title="Join Now" tabindex="0">Join Now</a>
</div>
</div>
</div><div class="offer col-md gtm-data slick-slide slick-active" data-gtm-id="5003" data-gtm-title="40 גיגה All included" data-gtm-price="29.25" data-gtm-category="חבילות" data-gtm-list="homepage" data-gtm-position="2" data-slick-index="1" aria-hidden="false" tabindex="0" style="width: 370px;">
<div class="upper">
<div class="title"><spam class="threshold">40</spam><spam class="units">GB</spam></div>
<div class="subtitle"><p>All included</p>
</div>
<!--<div ><span>test</span></div>-->
</div>
<div class="bottom">
<div class="price-area">
<div class="title"><spam class="price-number"><span class="number">29</span><span class="decimal">.25</span></spam> </div>
<div class="subtitle">
<span></span>
</div>
</div>
<div class="link">
<a href="en/userGuide/step1?packageidReg=5003&process=CustomerGuide" class="button red full gtm-click" title="Join Now" tabindex="0">Join Now</a>
</div>
</div>
</div><div class="offer col-md gtm-data slick-slide slick-active" data-gtm-id="5625" data-gtm-title="500 גיגה 5G Surfing" data-gtm-price="44.00" data-gtm-category="חבילות" data-gtm-list="homepage" data-gtm-position="3" data-slick-index="2" aria-hidden="false" tabindex="0" style="width: 370px;">
<div class="upper">
<div class="title"><spam class="threshold">500</spam><spam class="units">GB</spam></div>
<div class="subtitle"><p>5G Surfing</p>
</div>
<!--<div ><span>test</span></div>-->
</div>
<div class="bottom">
<div class="price-area">
<div class="title"><spam class="price-number"><span class="number">44</span></spam> </div>
<div class="subtitle">
<span><p>For the 2nd line. 1st line 49 ₪ /month</p>
</span>
</div>
</div>
<div class="link">
<a href="en/userGuide/step1?packageidReg=5625&packageidChange=5624&process=CustomerGuide" class="button red full gtm-click" title="Join Now" tabindex="0">Join Now</a>
</div>
</div>