How to identify the element using Selenium and Java - 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.

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

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

Selenium NoSuchElementException on input field

I try to send Keys to an input field via Java Selenium. I get the NoSuchElementException every time. I also tried everything from this Solution: NoSuchElementExeption, selenium unable to locate element. Thanks in Advance!
driver.findElement(By.xpath("//input[#class='pull-left ng-pristine ng-validng-empty ng-touched']")).sendKeys(t + Keys.ENTER);
<input class="pull-left ng-pristine ng-valid ng-empty ng-touched" ng-model="TagInputCtrl.tagInput" uib-typeahead="tagSuggestion for tagSuggestion in TagInputCtrl.getTagSuggestions($viewValue)" select-on-comma="" select-on-whitespace="" select-on-blur="" typeahead-focus-first="false" tag-select="TagInputCtrl.onEnter" tag-select-model="ngModel" sprd-max-input-length="50" ng-show="ngModel.length < TagInputCtrl.validatorOptions.tags.max" ng-focus="TagInputCtrl.focused = true" ng-blur="TagInputCtrl.focused = false" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-4377-3960" style="" type="text"/>
To send a character sequence as the desired 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:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.pull-left.ng-pristine.ng-valid.ng-empty.ng-touched[ng-model^='TagInputCtrl']"))).sendKeys("Tim");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='pull-left ng-pristine ng-valid ng-empty ng-touched'][contains(#ng-model,'TagInputCtrl')]"))).sendKeys("Tim");
There can be two things for NoSuchElementException :
Your locator is not correct.
If this is the case then you can try with this xpath :
//input[contains(#class,'pull-left ng-pristine ng-valid ng-empty ng-touched') and #ng-model='TagInputCtrl.tagInput' and #ng-focus='TagInputCtrl.focused = true']
Your webpage is integrated with Angular. So the provided xpath should work.
Your input tag might be in iframe/frame/frameset.
If this is the case, then I would recommend you to switch the focus of your driver to the particular iframe, to interact with desire element.
for switching you can try this code :
driver.switchTo().frame(name_or_id)
Generally, iframe tag contains name or id attribute, in case if both of them aren't available , you can go ahead with
driver.switchTo().frame(index)
or
driver.switchTo().frame(iframe_element)
Here, iframe_element is a web element.
Hope this helps.

Selenium, JAVA - how to find such element?

Do you know how to find and click at an element like this:
<div class="offer ctrlOpenOfferInfo">
<a class="linkOffer" href="offer_view.html?id=1007"/>
<p class="photo">
<p class="name">New offer</p>
<p class="price">123</p>
<div class="rate ctrlViewRateOffer" data-value="0.0000">
<p class="date"/>
<div class="hide info">
<div class="bgInfo"/>
using Selenium WebDriver and using the name of the element as there are few very similiar elements on the page?
Here's what I've tired so far:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[text()='New offer']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[#id='formDevelopmentElementAdd'][text()='New offer']")));
driver.findElement(By.xpath(".//*[#id='formDevelopmentElementAdd']/div/p[2][text()='New offer']")).click();
or:
driver.findElement(By.xpath("//p[#class='name'][text()='New offer']")).click();
or:
String address = driver.findElement(By.xpath("//*[contains(text(), 'New offer') and #class='name']")).getAttribute("href");
driver.get(baseUrl + address);
or:
driver.findElement(By.xpath("//a[Text()='New offer']")).click();
I've tried these with text, class, xpath...
Assuming you want to click the p tag containing New offer:
I would think this element is not inside an iframe. If so, you must use driver.switchTo().frame("name or xpath or css for frame locator"); first before start looking for element.
Now, how can you find that element. Consider the following options:
Using xpath text based search
//p[.='New offer']
//to be more precise you can do
//p[#classs='name'][.='New offer']
You can use By.className('name') to identify that element even though that name does not look like unique to me
Using cssSelector
.name
using nth-child() function of cssSelector
.offer.ctrlOpenOfferInfo>a>p:nth-child(2)

Categories