How to click on button Apply Now - java

HTML:
Apply Now
I have already tried the following:
driver.findElement(By.xpath("//a[text()='Apply Now']")).click();
driver.findElement(By.xpath("(.//[#href='Apply Now'])")).click();
driver.findElement(By.xpath("//a[#href='Apply Now']")).click();
driver.findElement(By.linkText("Apply Now")).click();
driver.findElement(By.className("Apply Now")).click();
driver.findElement(By.xpath("//div[contains(#class,'btn btn-success btn-lg')]")).click();
driver.findElement(By.xpath("//div[contains(#class,'btn btn-success btn-lg')]")).click();v

Try to click with webdriver wait till element is properly loaded in Dom and ready to receive click.
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[.='Apply Now']"))).click();

Option 1
Try By.CssSelector
Option 2
Not sure if you are checking if element is exists before clicking on it.if not then use wait.until.
If possible paste error you are getting here.
Hope this helps.

The desired element is a dynamic element so to locate and 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("Apply Now"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.btn.btn-success.btn-lg[href*='associateregistration']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='btn btn-success btn-lg' and contains(#href, 'associateregistration')][text()='Apply Now']"))).click();

Try this please, maybe it will help
driver.findElement(By.id("select2-section-tn-container")).click();

Related

How to click on a radio button using Selenium

I'm trying to click on a radio button in selenium java but it's not clicking.
This is the inspect on the radio button:
<input name="timeSyncType" class="radio" id="radioNTP" type="radio" ng-click="oSettingTimeInfo.szTimeMode='NTP'" ng-checked="oSettingTimeInfo.szTimeMode=='NTP'">
I tried this
WebElement radio = driver.FindElement(By.id("radioNTP"));
radio.click();
also I tried this
WebElement rbutton = driver.findElement(By.xpath("//input[#id='radioNTP']"));
rbutton.click();
but no luck, any help?
Firs you will have to make sure that this id radioNTP is unique in HTMLDOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the //input[#id='radioNTP'] and see, if your desired element is getting highlighted with 1/1 matching node.
If yes, then there are quite a few ways to perform click in Selenium.
Code trial 1:
Thread.sleep(5);
driver.findElement(By.xpath("//input[#id='radioNTP']")).click();
Code trial 2:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='radioNTP']"))).click();
Code trial 3:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//input[#id='radioNTP']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button);
Code trial 4:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//input[#id='radioNTP']"));
new Actions(driver).moveToElement(button).click().build().perform();
If No, then you will have to change the locator and use the one which represent the desire node.
PS: Thread.sleep(5); is just to solve this problem, if any of the above code works, Please replace the web element with WebDriverWait.
To click on the element you can use either of the following locator strategies:
cssSelector:
driver.findElement(By.cssSelector("input#radioNTP[name='timeSyncType'][ng-click^='oSettingTimeInfo']")).click();
xpath:
driver.findElement(By.xpath("//input[#id='radioNTP' and #name='timeSyncType'][starts-with(#ng-click, 'oSettingTimeInfo')]")).click();
However, as the element is an Angular element so ideally to click() on the element you need 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("input#radioNTP[name='timeSyncType'][ng-click^='oSettingTimeInfo']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='radioNTP' and #name='timeSyncType'][starts-with(#ng-click, 'oSettingTimeInfo')]"))).click();

Selenium xpath not clicking on Image link

Im trying to use selenium to select the first result in a search.
To click on the first image im using xpath way to find the first result from the search.
The code is
driver.findElement(By.xpath("//*[#id='category-search-wrapper']/div[2]/div/ol/li[1]/article/a")).click();
and from using the f12 and then ctrl f tools on the website it shows that I have the correct xpath
However, it is not clicking on the Image.
Here is the website that I am trying to test if it's any help.
https://www.dunnesstores.com/search?keywords=lamp
To click() on the first result from the search you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
driver.get("https://www.dunnesstores.com/search?keywords=lamp")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ol.product-list.product-list-main > li a"))).click();
xpath:
driver.get("https://www.dunnesstores.com/search?keywords=lamp")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#id='onetrust-accept-btn-handler']"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ol[#class='product-list product-list-main']/li//a"))).click();
Browser Snapshot:
driver = webdriver.Chrome()
driver.get('https://www.dunnesstores.com/search?keywords=lamp')
accept = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,
"#onetrust-accept-btn-handler")))
accept.click()
driver.find_element_by_css_selector(
'[class="c-product-card__link"]').click()
You have to first click accept cookies , then find element using class or xpath or css
Try:
element = driver.findElement(By.xpath("//*[#id='category-search-wrapper']/div[2]/div/ol/li[1]/article/a"))
driver.executeScript("arguments[0].click();", element)
With this code you avoid the elements that obscures it and the elements that are not disponible in your actual screen (out of scroll).

Selenium to click this button

I am trying to create a test to select this button
<button type="submit" class="btn btn--stretch btn--primary btn--green t-auth__login--btn">
Create my account</button>
the code that I used is this, but it doesn't work
driver.findElement(By.className("btn btn--stretch btn--primary btn--green t-auth__login--btn")).click();
Can somebody tell me the correct way to code this test?
If it is any help, the webpage in question is https://www.dunnesstores.com/customer/login
To click() on the element you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("button.btn.btn--stretch.btn--primary.btn--green.t-auth__login--btn")).click();
xpath:
driver.findElement(By.xpath("//button[#class='btn btn--stretch btn--primary btn--green t-auth__login--btn' and contains(., 'Create my account')]")).click();
Ideally to click() on the element you need 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("button.btn.btn--stretch.btn--primary.btn--green.t-auth__login--btn"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--stretch btn--primary btn--green t-auth__login--btn' and contains(., 'Create my account')]"))).click();

How to click on the element as per the HTML which is within a modal box through Selenium?

I'm automating some website in which I need to log out. I'm facing a hard time in this code:
WebDriverWait wait = new WebDriverWait(d, 10);
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user logout")));
Category_Body.click();
d.findElement(By.id("logout_user")).click();
Thread.sleep(1000);
HTML:
<a class="user logout" title="Sign out" data-target="#confirm_popup" data-toggle="modal"></a>
Error:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"user logout"}
Try the following code for that:
WebDriverWait wait = new WebDriverWait(d, 10);
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".user.logout")));
Category_Body.click();
PS: You can do this with ExpectedCondition.elementToBeClickable, also.
Hope it helps you!
I think the issue is with the identifier
You have used
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user logout")));
But according to your HTML
<a class="user logout" title="Sign out" data-target="#confirm_popup" data-toggle="modal"></a>
The link have no id called "User Logout"
With out using id try to use class By.findElementByClassName("user logout")
As a Second solution, try to use xpath ( which will work most of the time )
If both the solutions are not usable you can use the JavascriptExecutor ( The elements which are hard to capture can be easily handled with JavascriptExecutor )
NOTE: The main issue is with you using "user logout" when there is no such ID
Cheers
As per your to locate the desired element to logout within the Model Box you need to induce WebDriverWait for the element to be clickable and you can use either of the the following options:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.user.logout[title='Sign out'][data-toggle='modal']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='user logout' and #title='Sign out'][#data-toggle='modal']"))).click();

Getting Selenium "Element not clickable" issue

I am not able to click the element. When I am executing my test in a computer it runs perfectly, but when I am executing my test in a laptop it is failing. Getting error Element not clickable. I have tried to use different wait times as well. Have no idea where is issue. This is what I am using :
WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#ng-click='startExam()']"))).click;
Action action=new Actions(driver);
actions.moveToElement(ele).perform();
action.movetoElemet(ele).click().perform.
This what I have in inspect console:
<button class="btn btn-primary ng-scope" ng-if="!proctoredSession" ng-click="startExam()" ng-dissabled="!isExamContentLoaded">Start Exam</button>==$0
Try this:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.findElement(By.cssSelector("button[ng-click='startExam()']"));
or
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[ng-click='startExam()']")));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true)",button);
button.click();
As per the HTML within your question the element is an Angular element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following solution:
CSS_SELECTOR:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-primary.ng-scope[ng-click^='startExam']"))).click();
XPATH:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn-primary ng-scope'][contains(.,'Start Exam')]"))).click();
You may update your existing code as following:
WebDriverWait wait=new WebDriverWait(driver,30);
Action action=new Actions(driver);
WebElement temp = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#ng-click='startExam()']")));
action.moveToElement(temp).click().perform();

Categories