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 class="comment"><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">
Join Now
</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 להצטרפות 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.
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();
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();
Related
I am a beginner in selenium, I am using java for that, I am getting a error saying Unable to locate element although that element is present in the web page.
Java Code:
WebDriver dChromedriver=new ChromeDriver();
dChromedriver.get("https://erp.mitwpu.edu.in/");
dChromedriver.manage().window().maximize();
WebElement txtPassword=dChromedriver.findElement(By.id("txtPassword")) ;
WebElement txtUserId=dChromedriver.findElement(By.id("txtUserId")) ;
txtUserId.sendKeys("S1032200787");
txtPassword.sendKeys("FynS#3XbZH6ZMWH");
//
WebDriverWait w =new WebDriverWait(dChromedriver, Duration.ofSeconds(30));
w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#id='ReCaptchContainer']")));
dChromedriver.findElement(By.xpath("//div[#id='ReCaptchContainer']")).click();
// Thread.sleep(3000);
w.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("label#lblMessage")));
WebElement success_lablElement=dChromedriver.findElement(By.cssSelector("label#lblMessage"));
Assert.assertEquals(success_lablElement.getText(),"Success" );
dChromedriver.findElement(By.id("btnLogin")).click();
WebElement resultWebElement=dChromedriver.findElement(By.xpath("//nav[#class=\"mt-2\"]/ul/li[12]/a[#href=\"Examination/Report/StudentGradeCardDetail.aspx?MENU_CODE=Web_Result\"]"));
resultWebElement.click();
Thread.sleep(30000);
dChromedriver.findElement(By.cssSelector("input#btnshow")).click();
html code:
<div class="labelinfo col-md-12 col-sm-7 col-xs-12 padd6"> <input type="submit" name="btnshow" value="Show" onclick="return funValidation();" id="btnshow" class="btn btn-primary" style="">
<input type="submit" value="Show" onclick="return funValidation();" id="btnshow" class="btn btn-primary" style="">
</div>
The element Show button is inside an iframe.
In order to access the element, you need to switch to iframe first and then you can access the element.
Use WebDriverWait() and wait for frameToBeAvailableAndSwitchToIt() and wit for elementtobeclickable() to click on the Show button.
WebDriverWait w =new WebDriverWait(dChromedriver, Duration.ofSeconds(30));
w.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("FrameContent")));
w.until(ExpectedConditions.elementToBeClickable(By.id("btnshow"))).click();
To jump out from iframe you need to use this code.
dChromedriver.switchto().defaultcontent();
Snapshot iframe:
Need some help on Xpath for following code:
<span class-"metadata-row float-left" style="width: 1.9vw;"> &absp; </span>
<input placeholder="New Course Name" id="newCourseName" type="text” class="metadata-name metadata-name-edit font-12" autofocus>
<span class="fa fa-check metadata-action-icon" title="Save" onclick="addCourse(this)" style="display: block;"> ... </span>
I want to click on "Save" which is mentioned as title in the code but in ui it is showing as icon.
To click() on the element with title attribute as Save you need to use elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.fa.fa-check.metadata-action-icon[title='Save']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='fa fa-check metadata-action-icon' and #title='Save']"))).click();
try javascript executor
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
The Element is inside tag but many ways tried couldn't click on it.
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#value='Login to Register']"))).click();
and
WebElement element = driver.findElement(By.xpath("//input[#value='Login to Register']"));
JavascriptExecutor jsEx= (JavascriptExecutor)driver;
jsEx.executeScript("arguments[0].click();", element);
Here is the html:
<div id="requestRegistrationWidget">
<a id="requestLocationLogin" href="/user/login?destination=%2Fsearch%2Flocations%2Fwidget%3Fparam1%3D006046-0619_1565278200_11000000%26param2%3D3" class="use-ajax login-popup-form" data-dialog-type="modal">
<input class="btn btn-primary" style="width: 100% !important;" type="button" value="Login to Register"></input>
</a>
<!-- registerSession == 3 and registerAnyActiveSession == 1 case (2) -->
</div>
Can you give a try with the below code.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("requestLocationLogin"))).click();
I suppose you are facing this issue on chrome.
Try this, it works for me.
element.sendKeys(Keys.RETURN);
Its just hitting the "Enter" button on the element.
To click() on the element with text as Login to Register as the element is AJAX enabled element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.use-ajax.login-popup-form#requestLocationLogin>input.btn.btn-primary[value='Login to Register']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='use-ajax login-popup-form' and #id='requestLocationLogin']/input[#class='btn btn-primary' and #value='Login to Register']"))).click();
I got the problem with an XPath.
I can use this xpath: //*[#id='name']//*[#class='class']//div[1] -> can run ok.
But I want use div[2] ex: //*[#id='name']//*[#class='class']//div[2] , it gives error that element is not visible.
Anyone help me plz, I don't know why div[1] can run but div[2] is not visible.
My HTML code here:
<div class="class">
<div class="action-item" data-id="24" data-actioncode="STT">
<i class="fa fa-play"></i>
S T T
</div>
<div class="action-item" data-id="29" data-actioncode="FULL">
<i class="fa fa-play"></i>
FULL
</div>
<div class="action-item" data-id="30" data-actioncode="TEACHER">
<i class="fa fa-play"></i>
TEACHER
</div>
</div>
Code I have tried:
WebElement btnElement = driver.findElement(By.xpath("//[#id='name']//[#class='class']//div[2]"));
WebDriverWait wait= new WebDriverWait(driver,10 );
wait.until(ExpectedConditions.visibilityOf(btnElement));
btnElement.click();
You can try out this code :
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#data-actioncode='FULL']")));
driver.findElement(By.xpath("//div[#data-actioncode='FULL']")).click();
As per the HTML you have shared to click on the element with text as FULL you have to induce WebDriverWait for the element to be clickable and you can use either of the following locators :
css_selector (attribute based) :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.class div.action-item[data-actioncode='FULL']"))).click();
xpath (attribute based) :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='class']//div[#class='action-item' and #data-actioncode='FULL']"))).click();
xpath (text based) :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='class']//div[#class='action-item'][normalize-space()='FULL']"))).click();
You can use CSS selector path as well like for div 2 it is :
div.action-item:nth-child(2)
Thanks.
You can make use of the action-item class to validate.
use x-path to identify the classname and then do an n-th child:
xpath("//[#id='name']//[#class='class']//[#class='action-item']:nth-child(2)"));
or you can use the index value as well, as these are inside the same class.
difference is, the index value for the second item is gonna be 1.
alternately, you can use cssContainText as well to identify by the text.
I want to Click the "OK" button during a Selenium test but the Element is not visible.
driver.findElement(By.xpath("//*[#id=\"5f6e7b16-0fa1-4db6-869b-3a6ba6b0fafe\"]")).click();
<div class="bootstrap-dialog-footer-buttons">
<button class="btn btn-default" id="5a4bb849-7a61-4603-9ef2-f9e0ecab4523">
<span class="glyphicon glyphicon-ban-circle"></span> Cancel
</button>
<button class="btn btn-warning" id="f7f4b18b-2ba2-4c1e-b541-a254c080f398">
<span class="glyphicon glyphicon-ok"></span> Ok
</button>
</div>
I think in your DOM, the button id is changing dynamically. Whenever page reload it will generating new id. There is different button id you used in your Selenium code and HTML. So, I suggest you go with className. Try below code and hope it works for you.
//If the Element is not visible then wait until that element is not visible
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.className("btn btn-warning")));
//If the element is visible but not Clickable then wait until that element get Clickable.
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.className("btn btn-warning")));
//Then simply click the button
driver.findElement(By.className("btn btn-warning")).click();
Use JavascriptExecutor to click the element,
Refer code,
WebElement element = driver.findElement(By.xpath("//*[#id=\"5f6e7b16-0fa1-4db6-869b-3a6ba6b0fafe\"]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
As per the HTML you have shared it seems that the desired element is within a Bootstrap Modal Dialog Box and the id attribute of the element is dynamic. So to invoke click() you have to induce WebDriverWait as follows :
cssSelector :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.bootstrap-dialog-footer-buttons button.btn.btn-warning"))).click();
xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='bootstrap-dialog-footer-buttons']//button[#class='btn btn-warning']"))).click();