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

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

Related

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

Select Xpath of button

The following inspect code i have is to click the button I can confirm my order
<button type="submit" class="button btn btn-default button-medium">
<span>I confirm my order<i class="icon-chevron-right right"></i></span>
</button>
<span>I confirm my order<i class="icon-chevron-right right"></i></span>
The one I've tried:
driver.findElement(By.xpath("//button[contains(text(),'I confirm my order')]\"")).click();
The error is coming out using Eclipse as IDE for Selenium Testing stated below:
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //button[contains(text(),'I confirm my order')]" because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//button[contains(text(),'I confirm my order')]"' is not a valid XPath expression.
Try this:
driver.find_element_by_xpath('//button[#type="submit"]/span[1]').click()
The text I confirm my order is within the child <span> of the <button> element. So, to click() on the element you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("button.button.btn.btn-default.button-medium > span i.icon-chevron-right.right")).click();
xpath:
driver.findElement(By.xpath("//button[#class='button btn btn-default button-medium'][./span[contains(., 'I confirm my order')]]")).click();
However, as the element is a JavaScript enabled 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:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.button.btn.btn-default.button-medium > span i.icon-chevron-right.right"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='button btn btn-default button-medium'][./span[contains(., 'I confirm my order')]]"))).click();

How to identify the element using Selenium and Java

I'm trying to identify the element. I want to identify the "address your concerns.". I am able to reach up to div tag but i need to reach the text in the div tag.
HTML:
<span class="Linkify">
<div>
<div>
address your concerns.
</div>
</div>
</span>
I am trying to identify the text "address your concerns" so can anyone help me with this in chrome browser, I am trying?
To identify the element using Selenium you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
cssSelector:
WebElement cssElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span.Linkify > div > div")));
xpath:
WebElement xpathElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#class='Linkify']/div/div[contains(., 'address your concerns')]")));
Try doing it using XPath to locate your div element, something like this "//span[#class='Linkify']/div/div" and then use getText() method on this div to get your text.

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

Unable to locate dynamic button on dialog box

I try to locate a dynamic button element after click search but it cannot locate it. The Create Account CID sometimes is not clickable and sometime can click.
<div class="pzbtn-rgt" data-click="...">
<div class="pzbtn-mid" data-click="....">
<img src="https://10.204.137.86:5111/prweb/PRWebLDAP3/SstGGrXNazw%5B*/webwb/zblankimage.gif" alt="" class="pzbtn-i">
Create Individual CID
<img alt="" src="https://10.204.137.86:5111/prweb/PRWebLDAP3/SstGGrXNazw%5B*/webwb/zblankimage.gif" class="pzbtn-i">
The create individual CID is the one need clicks on.
I using absolute xpath but it still fails. I had tried with many ways. Please help. Thanks.
WebDriverWait waitCIDBtn = new WebDriverWait(driver, 10);
waitCIDBtn.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[3]/div[4]/div/div/form/div[1]/div/div/div/div/div[1]/table/tbody/tr/td/div[2]/table/tbody/tr/td/div/div/span/div/div[4]/div/div/div/div[4]/div/div/span/button/div/div/div/div")));
WebElement createCID = driver.findElement(By.xpath("/html/body/div[3]/div[4]/div/div/form/div[1]/div/div/div/div/div[1]/table/tbody/tr/td/div[2]/table/tbody/tr/td/div/div/span/div/div[4]/div/div/div/div[4]/div/div/span/button/div/div/div/div"));
createCID.click();
As you are trying to invoke click() on the element so, instead of using visibilityOfElementLocated() you need use elementToBeClickable() and you can use either of the following Locator Strategies:
Using cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.pzbtn-mid>img.pzbtn-i[src*='zblankimage']"))).click();
Using xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='pzbtn-mid']/img[#class='pzbtn-i' and contains(#src, 'zblankimage')]"))).click();

Categories