I am working with Selenium Web driver and need to reference am item in a javascript menu that has no id. I need to know how I can get the following web element in Selenium. I am not very proficient with javascript or HTML so any help is needed.
<td class="menu" colspan="2">
<a href="javascript:Redirect('marks',0);" class="menu">
Display Text Here
</a>
</td>
To get the element with text as Display Text Here you can use either of the following Locator Strategies:
LinkText:
"Display Text Here"
CssSelector:
"td.menu>a.menu"
XPath:
"//td[#class='menu']/a[#class='menu'][contains(.,'Display Text Here')]"
You can locate this element by xPath:
//a[contains(., 'Display Text Here')]
so in JavaScript it will be like this:
driver.findElement(By.xpath("//a[contains(., 'Display Text Here')]")).click();
Here you will find more information.
You can use this linkText :
Note that you should always choose linkText when compared to xpath and cssSelector.
driver.findElement(By.linkText("Display Text Here")).click();
Related
My actual query is, I want to retrieve the HTML inner text value using css selector or xpath. I am able to acheive this by using document.getElementById but not using selectors, instead I can only able to print the tag element but not the text from it.
For Ex:
<li class="active">
<span id="lastPrice">1,603.35</span>
<span id="CAToday"></span><br>
<span class="up" id="change">28.80</span>
</li>
From the above HTML, I want to print 1,603.35 using either
Css or
xpath
Note: I have drilled the forum already but couldn't able to find required solution.
This XPath expression
string(/li/span[#id='lastPrice'])
With this well-formed XML
<li class="active">
<span id="lastPrice">1,603.35</span>
<span id="CAToday"></span><br/>
<span class="up" id="change">28.80</span>
</li>
Result
1,603.35
Check in http://www.utilities-online.info/xpath/?save=07d6e884-4f7e-46cc-9aaf-904e6a440f50-xpath
You should be able to use the XPath version 2 matches function:
//div[matches(text(), 'Hello ?\w+ What would you like to do today \w+')]
which does allow regular expressions.
Java Selenium (xpath example):
driver.findElement(By.xpath("//span[#id='lastPrice']").getAttribute("innerText")
Python Selenium (CSS example):
driver.find_element_by_css_selector("span#lastPrice").get_attribute("innerText")
I am trying to search following text in HTML.
The file <b> abc.txt </b> is not available.
I have following xpath I am using
//div[contains(string(),'The file <b> abc.txt </b> is not available')]
But this is not working. If I use the following Xpath , it works though.
//div[contains(string(),'is not available')]
As I want to search for full text, please suggest how to handle this.
<b>...</b> is not a part of string representation, but child node of div. Try below XPath to match required div node:
//div[.="The file abc.txt is not available." and b="abc.txt"]
If it doesn't matter whether "abc.txt" is bold text, you can simplify to just
//div[.="The file abc.txt is not available."]
If "The file abc.txt is not available." is just a part of div text:
//div[contains(., "The file abc.txt is not available.")]
So, written code looks like this:
IWebElement Enter = webDriver.FindElement(By.LinkText("Enter"));
Im trying to get grab a Enter txt from code:
<li>
<a onclick="javascript:var p;if (document.getElementById('chckPot').checked==true){p=1;}else{p=0;};waitScreen();window.open('frmnewname.aspx?mod=0&pot='+p,'mojframe');" href="#">
<i class="fa fa-lg fa-folder-open">;</i>
Enter
</a>
</li>
So what ever I do I get the following error-message :
OpenQA.Selenium.NoSuchElementException: Unable to find element with LinkText == Enter
Code for test is written in C#, and previous version of app has a ID but new one don't have it (look in code).
Partial link text may help here (there could be extra newlines and spaces):
IWebElement Enter = webDriver.FindElement(By.PartialLinkText("Enter"));
Here is how you would approach this using xpath:
IWebElement element = webDriver.FindElement(By.XPath("//a[contains(text(), 'Enter')]"));
I am dynamically finding the string of open tag within a page and want to use JQuery to get the text of the element that the open tag corresponds to.
For example, suppose this is my page:
<h1 class="articleHeadline">
<NYT_HEADLINE version="1.0" type=" ">
The Arab Awakening and Israel
</NYT_HEADLINE>
</h1>
<author id="monkey" class="mainauthorstyle">
<h6 class="byline">
By
<a rel="author" href="http://topics.nytimes.com/top/opinion/editorialsandoped
/oped/columnists/thomaslfriedma/index.html?inline=nyt-per" title="More Articles
by Thomas L.Friedman" class="meta-per">
THOMAS L. FRIEDMAN
</a>
</h6>
</author>
I find the '<author id="monkey" class= "mainauthorstyle">' open tag string, and want to get $("author" id["monkey"] class["mainauthorstyle"]).text() --> By THOMAS L. FRIEDMAN. Is there a simple way to turn the open tag markup into a selector? Should I just parse it with regex?
Edit: I don't know beforehand what element I want. My code looks through the page and comes up with '<author id="monkey" class= "mainauthorstyle">' as the beginning of the element. I don't know the literal beforehand. I run this code on arbitrary webpages.
author#monkey.mainauthorstyle
Here's how you do it: http://jsfiddle.net/peduarte/MgbCX/
I'm having a bit of trouble understanding your question. If you just want the text, use the following:
$("author#monkey.mainauthorstyle").text();
If you want to select the element only if "THOMAS L. FRIEDMAN" is the author, then:
$("author#monkey.mainauthorstyle:contains('THOMAS L. FRIEDMAN')")
// See http://api.jquery.com/contains-selector/ for more info on the :contains() selector
UPDATED AFTER YOUR EDIT:
If you are able to determine that you want to select the "text" of the author element with id of "monkey" and class of "mainauthorstyle", you simply need to build a jQuery selector out of those bits of information. From there you can use the text() or html() methods on the resulting jQuery object to get the contents of that element.
As my example above showed:
$("author#monkey.mainauthorstyle").text();
or
$("author#monkey.mainauthorstyle").html();
Note that the selector used is probably overkill as you should be able to simply select the element by its id.
$("#monkey").text();
I am creating <td> and adding data to it.
$("<td>").addClass("tableCell1").text(mydata).appendTo(trow);
now if I have to create an <a> tag inside how to do it?
example:
<td headers="xx">
<a href="#" title="zz</a>
</td>
EDIT:
$("<td>").addClass("tableCell1").text(mydata).appendTo(trow);
is working fine but
$('a').attr({ href: '#', title: 'title here' }).appendTo($('td')).appendTo(trow);
is not working, I am not getting the <a> under <td>
I would do something like this:
$("td.tableCell1").append('');
EDIT: Or for even more safety in selecting the proper <td>, you should be able to use
$("td.tableCell1", trow).append('');
For even EVEN more safety, you could
$("td.tableCell1:lastChild", trow).append('');
I am assuming that you're trying to create the <a> and the <td> at the same time. If you're trying to add the <a> at a different event, you would need a different solution.
$("<td/>",{"class":"tableCell1", text:mydata})
.append($("<a/>",{ href:"#", title:"zz"}))
.appendTo(trow);
Note that you didn't indicate any content for the <a> in your question. If not styled properly, it will be invisible. If you want to add some text, add text: "some text" to the <a> creation.