Selenium to click this button - java

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();

Related

Selenium with Java: I can't find and element and click it

This is the button of the page I need to click:
<div _ngcontent-jnk-c107="" class="btn btn-primary btn-outline" tabindex="0" ng-reflect-router-link="/opportunity/1">VIEW</div>
I can't use the text view in the locator as there are other 22 buttons in the same page with the same text.
The only difference between them is that router link, which I don't know how to call. I already tried with Link text and partial link text, with no results.
In case ng-reflect-router-link attribute value is unique for this element you can use the following XPath locator:
//div[#ng-reflect-router-link='/opportunity/1']
To click() on the element with text as VIEW with respect to the ng-reflect-router-link attribute you can use either of the following Locator Strategies:
Using cssSelector:
driver.findElement(By.cssSelector("div.btn.btn-primary.btn-outline[ng-reflect-router-link='/opportunity/1']")).click();
Using xpath:
driver.findElement(By.xpath("//div[#class='btn btn-primary btn-outline' and #ng-reflect-router-link='/opportunity/1']")).click();
However, as the element is a Angular element, to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
Using cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.btn.btn-primary.btn-outline[ng-reflect-router-link='/opportunity/1']"))).click();
Using xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='btn btn-primary btn-outline' and #ng-reflect-router-link='/opportunity/1']"))).click();

How do I find an element with xpath?

I'm trying to find a "button" and click it but it has no id.
I tried using Xpath and even cssSelector but nothing works.
This are the elements of the button:
div style="cursor: grabbing;" title="Approved Forms" onclick="goToForms(0)" class="col-lg-2 col-sm-6"
I tried this :
driver.findElement(By.xpath("//div[contains(#title=,'Approved Forms')]")).click();
driver.findElement(By.xpath("/html/body/div[3]/div/div/div[2]/div[1]")).click(); *copy xpath*
driver.findElement(By.cssSelector(['title="Approved Forms"'])).click(); *has a sintax errors, not sure how to write it*
To locate the element you can use either of the following Locator Strategies:
cssSelector:
WebElement element = driver.findElement(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']"));
xpath:
WebElement element = driver.findElement(By.xpath("//div[#title='Approved Forms' and starts-with(#onclick, 'goToForms')]"));
Ideally, you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
cssSelector:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']")));
xpath:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#title='Approved Forms' and starts-with(#onclick, 'goToForms')]")));

How to click on button Apply Now

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();

Can't select option in Select with Selenium

I am having an issue when I try to select an option in a <select> in Selenium.
Select select = new Select(element);
actions.moveToElement(element);
select.selectByValue("100000");
This simply gives me ElementClickIntercepted.
Trying to click on it also gives me ElementClickIntercepted.
Trying to click on it with JS gives me a NullPointerException.
I can very easily select it in Firefox with the element selector so nothing is on top of the select that prevents me from clicking it.
What is intercepting the click? usually when it's because an element is overlaying another, it will tell me in the test results, but here it doesn't.
<div class="pull-left">
<select name="nb" class="form-control">
<option value="10">10</option><option value="20">20</option><option value="50">50</option><option value="100000">All</option>
</select>
</div>
Select xPath:
//select[#name="nb"]
And it is the only select on the page.
Try this:
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//select[#name='nb']")));
Select select = new Select(element);
actions.moveToElement(element);
select.selectByValue("100000");
As the element is a <select> element ideally you need to use Select class. To invoke click() on the option with value as 1000 you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.form-control[name='nb']")))).selectByValue("100000");
xpath:
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[#class='form-control' and #name='nb']")))).selectByValue("100000");

Unable to locate the button or link using either xpath, id, name or css selector

Unable to locate the element using id/name/xpath/CSSSelector
Tried the below codes and both failed to give response
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\'form\']/p/button/span")));
driver.findElement(By.xpath("//*[#id=\'form\']/p/button/span")).click();
and
WebElement checkout = driver.findElement(By.xpath("//[#id=\'form\']/p/button/span"));
checkout.click();
HTML
<button type="submit" name="processCarrier" class="button btn btn-default standard-checkout button-medium" style="">
<span> Proceed to checkout <i class="icon-chevron-right right"></i> </span>
</button>
Try Following CSS Selector.
WebElement checkout = driver.findElement(By.cssSelector("button.standard-checkout span"));
checkout .click();
Or yon can use WebDriverWait and Css Selector.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout span")));
element.click()
Probably you are getting org.openqa.selenium.InvalidSelectorException because you should use * after // to match any node(tag) who has id=form or the specific tag name.
change it to //*[#id='form']/p/button/span
Or use more specific like
xpath : //button[#name='processCarrier'] equivalent CSS : button[name='processCarrier']
And use implicit/explicit wait to make element available in DOM to perform actions.
Presumably you will invoke click() on the <button> element, so you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout.button-medium[name='processCarrier']>span"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='button btn btn-default standard-checkout button-medium' and #name='processCarrier']/span"))).click();

Categories