trying to find this span and click on it. There are multiple objects on the page with the same ID. Need to find by data-margin
<span title="Add me!" onclick="addCalc(this)" id="chkSelectedPrice" class="glyphicon glyphicon-unchecked pointer-finger add-calc" data-productid="1534" data-margin="1.375" data-lpc="0" data-unadjustedplf="0.578" data-plf="0.578" data-isfixed="False" data-buyrate="0"></span>
I think you can achieve this through xpath. Selenium has built-in xpath support so it should be pretty easy to query for your element
https://www.guru99.com/xpath-selenium.html
Also, if you want to generate an xpath for elements on an existing website you can try this plugin
https://addons.mozilla.org/en-US/firefox/addon/truepath/
I haven't tested this but I think your xpath will be something similar to
span[#data-margin="1.375"]
Here is the xpath.
//span[#data-margin="1.375"]
CSS:
span[data-margin="1.375"]
Related
I have delt with an Href button in the past and didn't find it too hard, but this button is being a pain. I have tried clicking by xpath, class, and link text. None have worked. I know there are plenty of the same question out there, but most of them give answer's that I am already trying. Below is the code I have. The one thing I haven't tried is javascriptexecutor. I also have been clicking on it in the command line of chrome and it does work. Just can't get selenium too. It throws an element not found. Also I feel it is worthy to note that I did not find any IFrames that I need to switch to. The only things that concern me which maybe I do not have the knowledge of selenium to deal with is the HTML mentions header, main, section, div, ul, li , and a which I have seen all before except for ul and li. Thank you for any help someone provides.
wait.Until(ExpectedConditions.ElementToBeClickable(By.ClassName("card-header-link float-md-right"))).Click();
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[1]/main/section[1]/div/ul/li[5]/a"))).Click(); //full xpath
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[#id='app']/main/section[1]/div/ul/li[5]/a"))).Click();
wait.Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Security"))).Click();
HTML
<li data-v-91f16f3e="">
<a data-v-91f16f3e="" href="/security" class="">
<span data-v-91f16f3e="" class="icon icon-shield"></span>
<span data-v-91f16f3e="" class="text">Security</span></a>
</li>
try this xpath :
//span[text()='Security']/..
or
//span[text()='Security']/parent::a[#href='/security']
in code :
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[text()='Security']/.."))).Click();
but it is strange to know that By.LinkText("Security") did not work.
Update 1 :
try this css selector
div[class$='desktop'] li a[href$='security']
code :
wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div[class$='desktop'] li a[href$='security']"))).Click();
I see the href attribute value is lowercased security, not Security.
So, please try this:
wait.Until(ExpectedConditions.ElementToBeClickable(By.Xpath("//a[contains(#href,'security')]"))).Click();
I have been trying various locators and can't find a simple solution to locating the number (which is dynamic and changing each page load) inside this span in C# Selenium. I am just trying to locate the span itself to start with.
<span inventory="Livecount">129</span>
I get the NoSuchElement exception, i.e. unable to locate element.
Last context-related thing: this span and its number live inside of a widget, which I have been able to locate just fine. That widget doesn't have a unique class or id, but a custom tag-name, so I'm only able to locate it reliably using the FindElement(By.TagName("inventorySearch")), which works. If there was a way I could use a CSS selector to start with that widget element (the parent) - and then chain down to the span inside it (there's only one span in each instance, so it would be easy to locate), that would solve it. My problem there is, I don't know how to indicate tagname inside a Seleniums CSS selector (i.e. classes and Id's have their . # symbols - is there a tagname equivalent in C# Selenium? Thanks.
Answering the question in the title, it is possible to locate a <span> with the text '129 via XPath:
//span[text()='129']
However I suspect that this number will change, so you may also want to consider locating the <span> where its inventory attribute equals 'Livecount':
CSS Selector:
span[inventory='Livecount']
XPath:
//span[#inventory='Livecount']
Usage:
driver.FindElement(By.CssSelector("span[inventory='Livecount']"));
driver.FindElement(By.XPath("//span[#inventory='Livecount']"));
Paraphrasing your last question, you asked if it's possible to use a CSS selector with a custom tag-name. This is also possible.
Given the following HTML:
<inventorySearch>
<span inventory="Livecount">129</span>
</inventorySearch>
To select the <span> within the <inventorySearch> element, use the following CSS selector:
inventorySearch > span
This is also possible using CSS
JQuery has a :contains pseudo selector
i.e. span:contains('129')
You will need to use the Selenium NuGet Support Package to get this functionality.
Install-Package Selenium.WebDriver.Extensions
This gives access to the Sizzle JQuery Selector Engine which provides the pseudo selector support.
Goal:
Select the second link, from top, by using By.LinkText
<a class="ng-binding" href="#/test/id_var1">324 fff</a>
<a class="ng-binding" href="#/test/id_var2">44 gggg</a>
Problem:
In this context the link text 44 gggg is not static because the link text changes every time the page is refreshed.
My idea is to retrieve all class="ng-binding" and then use the second link as a linkText, but I don't know how do it?
The answer is simply that it is impossible. You cannot get an element by link text if this text is dynamic and you don't know the value.
You could select it using other methods though.
css-selector
a[class='ng-binding']
tag-name (not recommended as it is probably not unique)
a
class-name
ng-binding
xpath
//a[#class='ng-binding']
Find the elements in a list and pull out the one you need.
Another option is selecting the 2nd element immediately using xpath.
(//a[#class='ng-binding'])[2]
use the below as cssSelector:
a.ng-binding[href$='/test/id_var2']// this will return the second link.
if u want to get all the elements using the
ng-binding
class, use the below as cssSelector
a.ng-binding//return all elemtns using the ng-binding class
You can also use below xpath:
//a[#href="#/test/id_var2"]
Here's a simulation of the HTML I am trying to use my XPath on:
<div class="stream-links">
<div>
value I need
</div>
<div>
value I need
</div>
<div>
value I need
</div>
</div>
Now, when I use the XPath pattern //div[#class='stream-links']/div/a in my browser it selects the <a ...> node. Everytime I press enter it selects the next one, but when I use the pattern //div[#class='stream-links']/div/a/text() it gets stuck on the text of the first <a ...> node so when I press enter it does not move to the next. (Using Firebug plugin on FireFox btw to inspect element)
I'm coding a program in C# and the amount of divs under the parent div is a variable so I can't use //div[#class='stream-links']/div[number here]/a/text() because I need to get all of them.
My code for using the Xpath is HtmlNodeCollection NODECOL1 = MEDOC.DocumentNode.SelectNodes("//div[#class='stream-links']/div/a[1]");
So my questions are:
1) Is there a particular reason Firebug doesn't jump to the next <a...> or is it a 'bug' on the plugin's side?
2) Will my code work nevertheless or do I need to approach it in another way?
There're a few things not right with the rest of my code so I can't see if that part of my code actually works or not, wouldn't ask question 2 if I could test it myself right now.
For your HTML, this XPath selects three a elements:
//div[#class='stream-links']/div/a
This XPath selects three text nodes:
//div[#class='stream-links']/div/a/text()
This XPath selects one a element:
//div[#class='stream-links']/div/a[1]
My code for using the Xpath is HtmlNodeCollection NODECOL1 =
MEDOC.DocumentNode.SelectNodes("//div[#class='stream-links']/div/a[1]");
1) Is there a particular reason Firebug doesn't jump to the next
or is it a 'bug' on the plugins side?
//div[#class='stream-links']/div/a[1] only selects one a element.
2) Will my code work nevertheless or do I need to approach it in
another way?
There's a few things not right with the rest of my code so I can't see
if that part of my code actually works or not, wouldn't ask question 2
if I could test it myself right now.
That's not a reasonable question to ask given what you've shown us. Perhaps knowing what the above XPaths return will help you answer it for yourself.
there is an html codes like below :
<div class="class1 class2 class3">
<div class="class4 class5">
<span class="class6">GOAL STRING</span>
</div>
</div>
now i want to find that GOAL STRING use from HTMLAgilityPack.
how can i do that?
[with LINQ and without LINQ = please show us both ways]
thanks in advance
Well you can use xpath to get the span directly.
document.DocumentNode.SelectSingleNode("//div[#class='class1 class2 class3']/div[#class='class4 class5']/span[#class='class6']").InnerText;
This is a good resource for xpath specifically the table in the middle of the page:
http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C
Also on Google Chrome you can right click -> inspect element and then right click the element that shows up on the tree and click copy as Xpath to get a starting point. These expressions can usually be simplified.