How to locate element in selenium for href - angularjs

I am trying to locate the element for this:
<a ng-if="showLink && customer.partnerType == 2 && customer.isDirectCustomer" class="cp_text_link ng-binding ng-scope" ng-href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&orgName=Attunix" href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&orgName=ABC">
ABC
</a>
I tried the XPath and CssSelector but its unable to locate element.
Can someone pls help me locate the element
TIA

Simply use text:
driver.findElement(By.xpath("//a[contains(text(),'abc')]"));

It is quite hard to come up with an exact locator without seeing the full code of the page, you're trying to automate.
From what I can see so far it makes sense to stick to ABC text so try the following:
Partial Link Text
driver.findElement(By.partialLinkText("ABC"));
Or the equivalent XPath
driver.findElement(By.xpath("//a[contains(text(),'ABC')]"));

Try using class
driver.findElement(By.cssSelector("a.cp_text_link"))

The desired element is an Angular element so to locate and invoke click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Java based Locator Strategies:
partialLinkText:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("ABC"))).click();
cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.cp_text_link.ng-binding.ng-scope[ng-href*='orgId'][href$='ABC']"))).click();
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='cp_text_link ng-binding ng-scope' and contains(#ng-href, 'orgId')][contains(#href, 'ABC')]"))).click();

Related

Having difficulty locating an element using ID, XPATH and CSS

Unable to locate an anchor element present inside an li element using Selenium webdriver.
For the below HTML DOM structure, I am unable to locate the second li element. I basically want to locate it and perform click operation on it there by loading in a new page using Selenium Webdriver.
Tried to access it using the below methods:
id - docTab
xpath -.//div[#class='secondary-tabs']/ul/li[1]/a
xpath - //*[#id="docTab"]
xpath - /html/body/div[1]/main/div[2]/div/div/div[2]/div[1]/ul/li[2]/a
css - #docTab. tab__heading
css - li[id="docTab"]
But all of it are resulting in "Unable to locate element" error. This is happening even after including driver.wait methods.
DOM Structure:
<div class="secondary-tabs">
<ul class="tabs tabs--bordered">
<li class="tab active second_tabs" id="textTab" tabdivid="textSecond"><a><div class="tab__heading" title="Text">Text</div></a></li>
<li class="tab second_tabs" id="docTab" tabdivid="docSecond"><a><div class="tab__heading" title="File">Document</div></a></li>
<li class="tab second_tabs" id="faqTab" tabdivid="faqSecond"><a><div class="tab__heading" title="FAQ" onclick="FAQ">FAQ</div></a></li>
</ul>
</div>
I expect to locate the second li element and perform click event on it using selenium web driver.
To click() on the element with text as Document you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
Java:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.secondary-tabs>ul li#docTab[tabdivid='docSecond']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='secondary-tabs']/ul//li[#id='docTab' and #tabdivid='docSecond']"))).click();
You can try below given selector.
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='secondarytabs']//child::ul//child::li[#id='docTab' and tabdivid='docSecond']"))).click();

How to locate the element using Selenium Java

<input name="txtAnswer" class="box1" id="txtAnswer" type="text" maxlength="20">
My code:
driver.findElement(By.name("txtAnswer")).sendKeys("green");
To locate the desired element you can use either of the following Locator Strategies:
Using cssSelector:
driver.findElement(By.cssSelector("input#txtAnswer[class^='box'][name='txtAnswer']")).sendKeys("green")
Using xpath:
driver.findElement(By.xpath("//input[starts-with(#class, 'box') and #id='txtAnswer'][#name='txtAnswer']")).sendKeys("green")
Update
As you are seeing the error Unable to locate element you need to induce WebDruverWait for the element to be clickable and you can use either of the following solutions:
Using cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#txtAnswer[class^='box'][name='txtAnswer']"))).sendKeys("green")
Using xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[starts-with(#class, 'box') and #id='txtAnswer'][#name='txtAnswer']"))).sendKeys("green")
Id and class are two attributes for an web-element. to identify web element uniquely(one) id is used and to identify web elements use common properties like class, tag name etc..
so writing xpath with id attribute will give you unique match.
Answer: driver.findElement(By.id("txtAnswer")).sendKeys("green");

building xpath with #document tag in html

How can I build the xpath for Try it button from this website:
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert
driver.findElement(By.xpath("//div[#id='iframewrapper']/iframe/????")).click();
Also I tried this xpath //body[#contenteditable='false']/button which I know that isn't recommended. In Chrome console apear ok, but in Selenium doesn't find it and I don't know why.
Write this code, First you need to switch to Frame and then you need to click that button
driver.switchTo().frame("iframeResult")
driver.findElement(xpath: "//button[text()='Try it']").click()
In case if you want to use WebDriverWait :
WebDriverWait wait = new WebDriverWait(driver, 20)
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Try it') and #onclick='myFunction()']"))).click();

Finding a link in selenium webdriver

<div class="list_details">
<p class="lnk_primary show_inline" rv-text="config.app.message.Label.abc.SmallScreen.58384" rv-on-click="current.eventGoToName">Add name</p>
</div>
I tried finding the link 'Add name' by using 'linktext' and 'partiallinktext' but it throws unable to locate element. I tried using classname but it also failed. Finally i used xpath to work.
My Code:
driver.findElement(By.linkText("Add name")).click();
driver.findElement(By.partiallinkText("name")).click();
driver.findElement(By.className("lnk_primary show_inline")).click();
Please let me know whether am i making any mistake?
Sample program to find link using linktext :
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=2FqBVNuuJOzV8gea24GwDA&gws_rd=ssl#q=google");
driver.findElement(By.linkText("Google")).click();
please share your site URL so I can check.
By.linkText and By.partiallinkText are only looking for the
<a> tag. Since your link is in a <p> tag, it will not be found. Finding the element by xpath, like you have been able to do, is the better solution!
Based off experience and this question, By.className will not work for finding elements with more than one class. You could use xpath again or By.cssSelector instead to locate the element, such as:
By.cssSelector('.lnk_primary.show_inline')

how to locate an element by value using css selector in webdriver?

For the following element, how to find it by the value of the td using css selector? In this case it's "unique text"
<td class="someclass" colspan="3">
unique text
</td>
You could use something like this,
With CSS Selector,
By.cssSelector("td[class='someclass'][value='unique text']");
For more information on using css selector, See here
We can create a XPath something like the below:
//td[contains(text(), 'unique text')]
Using the following XPath always gives me expected result and performance. See my another answer here
//td[.='unique text']
WebElement element =
driver.findElement(By.cssSelector("input[value='value to be taken']")); //This line is to select Radio Button
element.click();
You may use the CSS method to locate the element by its class name.
css=".someclass"
See more examples here.
You can locate the WebElement either using dynamic xpath or dynamic css
css-
WebElement css = driver.findElement(By.cssSelector("td#someclass"));
xpath-
WebElement xpath = driver.findElement(By.xpath("//td[text,'unique text']"));

Resources