Finding a link in selenium webdriver - 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')

Related

How to locate element in selenium for href

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

XPath for the element with ng-click

I am trying to find a button and click on it. I am using selenium with c# and also have protractor. I am not able to find the element.
<button class="btn--primary quote-summary__card-btn" role="button" aria-label="Choose Essential Cover" ng-disabled="!quoteSecondaryOptionsCtl.pageLoaded" ng-click="quoteSecondaryOptionsCtl.selectCover('Essential', 0)" style="">Choose</button>
could anyone please help me in this?
Try following XPATH.
//button[#class='btn--primary quote-summary__card-btn' and text()='Choose']
It really depends how your page is structured. Do you have any unique identifiers other than the button text? If not, then locate it with XPath:
//button[contains(text(), 'Choose')]
If you want to locate it by your ng-click param then you can use css selector:
button[ng-click="quoteSecondaryOptionsCtl.selectCover('Essential', 0)"]

Unable to locate a web element using protractor in jasmine framework

I tried to locate an element on the page with the following but couldn't do.
element(by.css('.organizer-text.ng-binding')).click();
element(by.className('organizer-text')).click();
element(by.linkText('All Cases(1)')).click();
element(by.css('span[class="organizer-text"]')).click();
element(by.css('span[ng-class="{'folder-selected' : isSelected(node)}"]')).click();
element(by.css('span[title="All Cases (1)"')).click();
element(by.xpath('div//span[title()="All Cases(1)"]')).click();
Attaching the screenshot of the element with the DOM. Could you please help me on how to locate it?
<span class="organizer-text ng-binding"
tooltip="buildLabel(node.name, node.totalCases)"
ng-click="onLabelClick($event, node)"
ng-class="{'folder-selected' : isSelected(node)}"
ng-show="!node.showEditName" title="All Cases (1)">
All Cases (1)
</span>
The structure is div->span->multiple spans here (one of the spans here is the element)
try
element(by.xpath('//span[#title="All Cases(1)"]')).click();
I used browser.ignoreSynchronization = true; after logging into the page and it worked.
Looks like you found your answer yourself. But, this is for your own information. Just try this work flow out.
Right-click of your element and get the inspect element window.
Go to inspect element of the element and right-click
There will be a window with some options
Select copy Unique Selector option.
element(by.css('paste your Unique Selector here')).click();
Like step 5 paste your unique selector
Hope this helps. :)

Selenium Fluent WebDriver - Click does not work as expected

I have a pagination list, that is essentially constructed like so (I am using AngularJS):
<span id="latestResultsIndicator">
<ul>
<li ng-click="Item.action()"><span>1</span></li>
<li ng-click="Item.action()"><span>2</span></li>
</ul>
</span>
This works great in HTML, but when I try to write an integration test using Selenium WebDriver and the Fluent API I run into problems.
Specifically I want to click on the second li, to do this I am using the following code
I.Assert.Exists("#latestResultsIndicator");
var secondPageElement = I.Find("#latestResultsIndicator ul li:nth-child(2)");
I.Click(secondPageElement);
This doesn't actually work! If I use jQuery in the Chrome console to do $("#latestResultsIndicator ul li:nth-child(2)").trigger("click") then the second page is selected, so I know the selector is correct.
To test further I added a double click like so:
I.DoubleClick(secondPageElement);
What I noticed here is that the very first li gets selected. Its as if its trying to click or select the wrong one! (See the image).
What is happening?
I know Webdriver has some issues with css pseudo-selectors such as nth-child. You might be able to make it work with Xpath, which is just as fast and just as flexible.
var webDriver = (IWebDriver)I.Provider;
webDriver.FindElement(By.XPath("//*[#id='latestResultsIndicator']//ul//li[2]")).Click();
Not sure if it will work or not, as I have not used FluentAutomation.
The CSS selector as it is written in your question is not correct
"#latestResultsIndicator ul li:nth-child(2)"
The UL has the id you're looking for, so it should be
"ul#latestResultsIndicator li:nth-child(2)"
Not sure whether that is going to solve your problem, but it's a place to start. If not, please update with the results

Using Element's tool tip as Locator in Selenium Webdriver

Can we use button's tool tip as a locator in selenium WebDriver? I came across this question because none of the default locators provided in selenium webdriver are useful for my applications elements.
Elements in the application i am testing randomly changes its,
Id,
Xpath-pos,
Xpath-relative Id, and
CSS
everytime the page is Refreshed! This happens because the application is created in ExtJs and HTML 5.
Yes, you can. But please show your HTML code. There are no general answers.
For example, this ExtJS demo here. http://try.sencha.com/extjs/4.1.1/demos/Ext.Button.tooltip.1/
<button id="button-1009-btnEl" type="button" class="x-btn-center" hidefocus="true" role="button" autocomplete="off" data-qtip="I'm a custom tooltip" style="height: 16px;">
<span id="button-1009-btnInnerEl" class="x-btn-inner" style="">Button w/ QuickTip (qtip) tooltip</span>
<span id="button-1009-btnIconEl" class="x-btn-icon "></span>
</button>
The tooltip is coded as data-qtip attribute. So you can use CSS Selector or XPath to find it.
XPath: //button[#data-qtip=\"I'm a custom tooltip\"]
CSS: button[data-qtip=\"I'm a custom tooltip\"]
Good question !
I would like to discuss it with some more experts but as far as my knowledge goes i dont think u can use tool tips as locators in webdriver!
Try this flow, may help you
don't bother if your script fails in IDE
Export it in webdriver/java/Junit
Try running the webdriver script now.
If the tooltip is in the HTML (which it must be), then you'll be able to use an XPath selector to find your element. Post a snippet of the HTML with the element you're trying to locate for more help

Resources