Im querying a span element (without class, id, or anything) using csspath in selenium c# but the text attribute is empty.
what's happening and how can I get the text within the span?
So the DOM structure is:
/div/div/ul/li/a/span
Query is:
driver.FindElement(By.CssSelector(".contactActions .iconActions .icon .privacy span"));
You could probably try
driver.FindElement(By.XPath("//div[#id='otherOuterDiv']//span"));
I usually use relative XPaths, because I find it easier to specify them.
Solution is:
string text = driver.FindElement(By.CssSelector(".contactActions .iconActions .icon .privacy span")).GetAttribute("textContent"));
Related
I have been working with selenium and C#, and I am wondering if I can return the text associated with a specific element.
For example, here is the line of HTML that contains what I want to display:
<span class="field">1149156-1</span>
Would it be possible to save the "1149156-1" in a string to use for later?
I have tried the following code, but it returns a strange value, definitely not the value I want it to return:
string testvariable = driver.FindElement(By.XPath("/html/body/div/div[4]/div[2]/form/div[2]/div/table/tbody/tr[2]/td[1]")).ToString();
Hope I provided enough information!
You can just use webElement.Text.
string testvariable = driver.FindElement(By.XPath("/html/body/div/div[4]/div[2]/form/div[2]/div/table/tbody/tr[2]/td[1]")).Text;
On another note -- I recommend using relative XPath syntax instead of absolute, and querying on WebElement attributes such as id, name, and class to get more accurate locators.
For example, the XPath in the example you provided can be re-written more robustly as such:
//table[#id='someId']/tbody/tr[2]/td[1]
I am using Selenium ChromeDriver. I have a problem obtaining the contents of a span as defined in the screen shot below:
I tried:
var text = element.Text;
but it returns an empty string.
The span's text is: "Yes, we can both go."
How can I get the span's text?
Edited to reflect updates in the comments
Because it's a span, which behaves much like a div element, there's no inherent text or value attribute at play--it's a container, and you'll need to grab the value from inside the span container (see this answer and this article). Based on the fact you want the actual string value and not the html, the "textContent" attribute is probably the best bet.
You can use Selenium's "GetAttribute" method to get the "textContent" attribute of the span:
var textValue = element.GetAttribute("textContent");
// do code against textValue
There's no direct get accessor built into the Element class for attributes, so you'll need to pass in the string value for the name of the attribute you want, and it'll return the value if it's present.
I have a .NET WebForm and I need to click a link using Selenium and can't use the text content (because of translation issues). How can I identify this element?
registration form
I have tried the following, which does not work:
var element = Driver.FindElementsByXPath($"//*[#href='ctl01']");
The problem is that you are trying to look for an id within the xpath, while the element does not contain an id.
In this case, this should work:
var element = Driver.FindElementsByXPath($"//a[contains(text(), 'registration form')]");
This will only work if all the elements which you are trying to find are links with the text registration form in it.
If you want to find elements on the href, use:
var element = Driver.FindElementsByXPath
("//a[contains(#href, 'javascript:__doPostBack('ctl01','')')]");
Ultimately decided to identify within the href attribute by partial string:
.FindElementsByXPath($"//*[contains(#href, '{id}')]")
This is because putting the whole value of the javascript text into the Selenium call caused it to fail parsing.
try searching for the a href instead of the id like this:
a[#href='javascript:__doPostBack('ctl01','')']
with FindElementsByXPath
then on the var element try using SendKeys like so:
element.SendKeys(Keys.Enter);
Hi I'm kind of new to selenium, so please bear with me if the question is too basic.
I wanna access a date picker element and choose a specific date.
I am trying to access the span element using both class and text inside.
I get an error of invalid string. Is the syntax below correct?
_driver.FindElement(By.CssSelector("span[class='xxx'][contains(text(),'xx')]"))
Looking at this cheatsheet
https://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/
I think it might be more like
_driver.FindElement(By.CssSelector("span.CCC:contains('TTT')"));
where CCC is your class name and TTT is the text your looking for.
I'm on a development process of a crawling engine. My program crawls websites through Xpath with HtmlAgilityPack. I need to get some image src tag's directly. You can see my simple code below which is not working correctly, thanks in advice!
PS: Please ignore " char problem, XPath patterns are provided by database.
Agility.DocumentNode.SelectSingleNode("//img[#id="product_photo"]/#src");
And this is the line i need to crawl (the *...* part shows block to extract
<img id="product_photo" src="*/images/thumb/4400/10280/st.jpg*">
Some pages provide image in meta tags so .Attributes["src"] wont work.
UPDATE: You can see my query and result here
You cann't get the value of "src" or any other attributes in using:
Agility.DocumentNode.SelectSingleNode(yourXpath);
Just by using:
string s=Agility.DocumentNode.SelectSingleNode(yourXpath).value;
It's because XPath cann't return value of an attribute by SelectSingleNode() func in HtmlAgilityPack class. So you must use SelectSingleNode(yourXpath).value or use Regex after the pharsing to get just the "src" without the outerText.