How to create an XPath for this, suitable for Selenium? - selenium

Please help me to create the Xpath for the following. I am using Selenium webdriver and have only IE browser. Following is the HTML:
<A
title=""
href="javascript:Plg({u:'/epace/starintfc.html?module=RUN_EVENT',p:0,nw:'0'});"
p="eagle/pace/pace"
la=""
ml="%09NULL%09PLUGIN%09%2fepace%2fstarintfc.html%3fmodule%3dRUN_EVENT%09%09%09%09%09%09%09%09%09%09"
t="Plugin" gh="" g="Data Steward"
pc="Eagle/PACE/PACE Components"
ivname=""
><IMG id=Item.Img
class=link
src="/tpe/modules/site/img/menu_plugin.gif">Run An Event</A>

As I prefer CSS over xPath because of it's simplicity, I will provide both the CSS and xPath solution:
Remember that you want to get the attribute of the elements that have the most specificity. The answers below are the best of my ability, to determine which have the most specificity. (see here if you want to learn more about CSS and specificity of elements)
If you are trying to select the first <a> element...
CSS:
a[href*='RUN_EVENT']
xPath:
//a[contains(#href, 'RUN_EVENT')]
If you are trying to select the <img> that is inside of the <a> tag...
CSS:
a[href*='RUN_EVENT'] > img
xPath:
//a[contains(#href, 'RUN_EVENT')]/img
My guess is that you want the first one since it's a link, and I assume you want to invoke a click on it.

Related

Choose the correct element from the list of objects with the same className

Quick one, i am trying to avoid using xpath and using css selectors due to performance issues xpath can have so i would like to know the right approach of locating for example "A" in the list
<div class="input-search-suggests" xpath="1">
<div class="input-search-suggests-item">A</div>
<div class="input-search-suggests-item">B</div>
<div class="input-search-suggests-item">C</div>
</div>
Currently i am locating A using xpath / span but it would be indeed sufficient locating all elements and then grabbing A from the list that have same class which is "input-search-suggests-item"
#FindBy(xpath = "//span[contains(text(),'A')]")
CSS_SELECTOR does not have support for direct text what xpath has.
What this means is, for the below xpath
xpath = "//span[contains(text(),'A')]"
based on text A you can not write a css selector.
Instead to locate A using css selector, you can do :
div.input-search-suggests > div.input-search-suggests-item
In Selenium something like this :
#FindBy(cssSelector= "div.input-search-suggests > div.input-search-suggests-item")
Even though it will have 3 matching nodes, but findElement will take the first web element.
Also you may wanna look at nth-child(n)
div.input-search-suggests > nth-child(1)
to make use of index to locate A, B, C
Here is the Reference Link

Unable to find CSS selector by :contains()

Unable to find CSS selector using :contains().
I have followed the instructions from https://www.browserstack.com/guide/css-selectors-in-selenium
at #5 – Inner text
but still, no result is shown, Can someone please help me/Tell me how to find Web element using text, CSS only
Here is Sample Dom
<ul id='id'>
<li class='class'>
<a class='class_class2' href="/Myaccount/summary">"Summary"</a>
<li class='class'>
<a class='class_class2' href="/Myaccount/Profile">"Profile"</a>
</ul>
Here : a:contains('Summary')
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
https://stackoverflow.com/a/4781167/6793637
Contains: is no longer available
you should use xpath to find elements using innerText
xpath:
//a[contains(text(),"Summary")]
You can get exact match as
//a[text()="Summary"]
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
You can find a couple of relevant detailed discussion in:
selenium.common.exceptions.InvalidSelectorException with "span:contains('string')"
Finding link using text in CSS Selector is not working
Alternative
To locate the element with text as Summary you can use either of the following css-selectors:
Using first-child:
ul#id li:first-child > a
Using nth-child():
ul#id > li:nth-child(1) > a
tl; dr
References:
CSS selector :contains doesn't work with Selenium 2.0a7
css pseudo-class :contains() no longer allows anchors

find specific element for button using selenium for web scraping

I am inspecting one button element from a web page using chrome driver and selenium. And the html code for the particular button is:
<div class="label text-left text-link link-blue text-
uppercase">Financial Statement Analysis <span class="count">(2)</span>
</div>
I have tried different element options like find element by name, xpath, link text etc. But none of them unable to locate the element.
What will be the element to locate the button. ?
try Xpath :
//span[contains(#class,'count') and text() = '(2)']
You can try with this css selector :
div.label.text-left.text-link.link-blue.text-.uppercase
To locate the element with text as Financial Statement Analysis (2) you can use the following solution:
Java Solution:
WebElement elem = driver.findElement(By.xpath("//div[#class='label text-left text-link link-blue text-uppercase'][contains(.,'Financial Statement Analysis')]"));

Selenium FindElement by parent div

Trying to find link element of "a href". Snippet code:
<div id="contact-link">
Contact us
</div>
I managed doing it by:
Driver.FindElement(By.XPath("//*[#title='Contact Us']")).Click();
2.Driver.FindElement(By.XPath("//a[#href='http://automationpractice.com/index.php?controller=contact']")).Click();
3.Driver.FindElement(By.XPath("//*[text()='Contact us']")).Click();
Could someone tell me how can I get by firstly getting parent div and then find what's inside that div (by going from the top to the bottom)
So basically, with xpath, you are looking to replicate the HTML structure. What you need is:
//div[#id='contact-link']/a
This is going to return the a href under the div. Assuming its just 1, thats the way to go. If you want to go a little further, try:
//div[#id='contact-link']/a[#title='Contact Us']
Although you have already accepted the answer , I would like to highlight some point about Xpath and cssSelector. You should always pick cssSelector over Xpath :
Here is cssSelector for your requirement:
div[id='contact-link']>a
Code :
Driver.FindElement(By.CssSelector("div[id='contact-link']>a")).Click();
For more about cssSelector : https://www.w3schools.com/cssref/css_selectors.asp
For Difference between Xpath and cssSelector, you can read it from this SO post: Diff between Xpath and cssSelector

how to locate element with selenium webdriver for below html

I have an issue clicking on the below HTML:
<div id="P7d2205a39cb24114b60b80b3c14cc45b_1_26iT0C0x0" style="word-wrap:break-word;white-space:pre-wrap;font-weight:500;" class="Ab73b430b430a49ebb0a0e8a49c8d71af3"><a tabindex="1" style="cursor:pointer;" onclick="var rp=$get('ctl00_ContentPlaceHolder1_ReportViewer1_ctl10_ReportControl');if(rp&&rp.control)rp.control.InvokeReportAction('Toggle','26iT0C0x0');return false;" onkeypress="if(event.keyCode == 13 || event.which == 13){var rp=$get('ctl00_ContentPlaceHolder1_ReportViewer1_ctl10_ReportControl');if(rp&&rp.control)rp.control.InvokeReportAction('Toggle','26iT0C0x0');}return false;"><img border="0" src="/Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=10.0.30319.1&Name=Microsoft.ReportingServices.Rendering.HtmlRenderer.RendererResources.TogglePlus.gif" alt="+"></a> 2013</div>
I have used the below script to click anchor inside a div tag. For the above html code it is not fixed only end part of id example "26iT0C0x0" is fixed. The script that I have used is:
WebElement e1=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[ends-with(#id,'26iT0C0x0')]/a")));
e1.click();
You can use the 'contains' method within an xpath lookup:
driver.findElement(By.xpath("//div[contains(#id,'26iT0C0x0')]")
I would recommend you to consider CSS selector alternative as CSS working faster, than xpath.
So 'contains' in attribute in CSS stands for '*=', for example
if we want to find attribute by 'CSS' ending in this: <htmlTag A="blablaCSS" > we need do the following:
String CSSselector="htmlTag[A*=CSS]";
and you get this element searched.
So considering your example CSS selector be like:
String cssSearched="div[id*=26iT0C0x0] a";
also try to click not on link - a
but on parent div as well:
String cssSearched="div[id*=26iT0C0x0]";
driver.findElement(By.cssSelector(cssSearched));
hope this works for you.
As Mark Rwolands already mentioned: the xpath-Function 'ends-with()' isn't supported in Selenium 2.
Also, if you maybe consider to use chromeDriver in the future, I would recommend clicking the image, not the anchor, see:
https://sites.google.com/a/chromium.org/chromedriver/help/clicking-issues
edit:
Also your IDs are looking generated. I wouldn't count on them for a stable test-environment.