there is two span tags in my page
and i tried that method
web.find_element_by_xpath('//span[#class="_54nh"]').click()
it goes to wrong span not what i want
so i need to select with 'Delete' text that is difference from the another tag
there is html source
▼<span>
<span class="_54nh">
</span>
and that what i need to select
▼<span>
<span class="_54nh">Delete</span>
</span>
the difference is the 'Delete' word
You can refine the xpath by adding the intended text in your search criteria as follows:
web.find_element_by_xpath("//span[#class='_54nh'][text()='Delete']").click()
Related
Using Selenium 4.8 in .NET 6, I have the following html structure to parse.
<ul class="search-results">
<li>
<a href=//to somewhere>
<span class="book-desc">
<div class="book-title">some title</div>
<span class="book-author">some author</span>
</span>
</a>
</li>
</ul>
I need to find and click on the right li where the book-title matches my variable input (ideally ignore sentence case too) AND the book author also matches my variable input. So far I'm not getting that xpath syntax correct. I've tried different variations of something along these lines:
var matchingBooks = driver.FindElements(By.XPath($"//li[.//span[#class='book-author' and text()='{b.Authors}' and #class='book-title' and text()='{b.Title}']]"));
then I check if matchingBooks has a length before clicking on the first element. But matchingBooks is always coming back as 0.
class="book-author" belongs to span while class="book-title" belongs to div child element.
Also it cane be extra spaces additionally to the text, so it's better to use contains instead of exact equals validation.
So, instead of "//li[.//span[#class='book-author' and text()='{b.Authors}' and #class='book-title' and text()='{b.Title}']]" please try this:
"//li[.//span[#class='book-author' and(contains(text(),'{b.Authors}'))] and .//div[#class='book-title' and(contains(text(),'{b.Title}'))]]"
UPD
The following XPath should work. This is a example specific XPath I tried and it worked "//li[.//span[#class='book-author' and(contains(text(),'anima'))] and .//div[#class='book-title' and(contains(text(),'Coloring'))]]" for blood of the fold search input.
Also, I guess you should click on a element inside the li, not on the li itself. So, it's try to click the following element:
"//li[.//span[#class='book-author' and(contains(text(),'{b.Authors}'))] and .//div[#class='book-title' and(contains(text(),'{b.Title}'))]]//a"
I'm new to Xpath so if this doesn't contain all relevant information apologies & let me know what you need to solve it.
I am trying to find an Xpath to an "expand arrow" element which expands a row in a table. The "expand arrow" element isn't unique so I would like to select it based on text contained in another element on the same row. The layout is as follows:
<td id="__table2-rows-row10-col0">
<div class="sapUiTableCellFlex">
<span id="__table2-rows-row10- treeicon" title="Expand Node" role="button">
<div id="__hbox27-__clone101">
<div id="__data70-__clone101">
<div id="__data71-__clone101">
<span id="__text47-__clone101" title="sys-admin">sys-admin</span>
I'd like to select the element with title = "Expand Node"
<span id="__table2-rows-row10- treeicon" title="Expand Node" role="button">
based on the element with title or text = "sys-admin"
<span id="__text47-__clone101" title="sys-admin">sys-admin</span>
I've played around with various options but I can't seem to get it to work. Any ideas would be much appreciated!
Thanks
To locate the element with title as Expand Node with respect to the element with title or text as sys-admin you can use either of the following Locator Strategies:
xpath and title attribute:
"//span[#title='sys-admin']//ancestor::span[#title='Expand Node']"
xpath and text() attribute:
"//span[text()='sys-admin']//ancestor::span[#title='Expand Node']"
I eventually got it to work using the following xpath:
//span[#title='sys-admin']/../../preceding-sibling::span[#title='Expand Node']
Given below is the snippet of HTML:
<div class="a-row a-spacing-none">
<span class="a-size-small a-color-secondary">by
</span>
<span class="a-size-small a-color-secondary">
<a class="a-link-normal a-text-normal" href="/Lowell-Fryman/e/B01M3MNJTE/ref=sr_ntt_srch_lnk_1?qid=1550228622&sr=1-1">
Lowell Fryman
</a>
and
</span>
<span class="a-size-small a-color-secondary">
<a class="a-link-normal a-text-normal" href="/Gregory-Lampshire/e/B01N7ZWT5Y/ref=sr_ntt_srch_lnk_1?qid=1550228622&sr=1-1">
Gregory Lampshire
</a>
</span>
</div>
I'm trying to obtain the name of all authors.
This is whatever test follows the word by.
I came up with the following XPath but it doesn't seem to fetch all the authors.
My XPath expression:
//div//span[text()=\"by \"]//following::span[1]//a
Can someone please tell me how to obtain the name of all the authors while somehow managing to skip any element whose text is "and"?
I'm using Selenium's find_element_by_xpath if it helps.
Try the below code.This should work.It will fetch all author.
elements=driver.find_elements_by_xpath("//a[#class='a-link-normal a-text-normal']")
for element in elements:
print(element.text)
Please let me know if this work.
Here xpath you can use to get authors:
//div[./span[normalize-space(.='by')]]//a
or
//div[./span[contains(.,'by')]]//a
Your xpath should be like this:
//span[normalize-space(.='by')]/following-sibling::span//a
you can first get text of all span elements in a list and then slice it from "by" text value
elements = [_.text() for _ in driver.find_elements_by_css_selector('div.span')]
print elements[elements.index('by'):]
To print the name of all the authors which are followed by the word by you can use either of the following solutions:
Using innerHTML:
print([author.get_attribute("innerHTML") for author in driver.find_elements_by_xpath("//span[contains(., 'by')]//following::span/a[#class='a-link-normal a-text-normal'][#href]")])
Using text:
print([author.text for author in driver.find_elements_by_xpath("//span[contains(., 'by')]//following::span/a[#class='a-link-normal a-text-normal'][#href]")])
DOM:
<span class="item">Does not <span class="highlight">match</span>.</span>
<span class="item">Also not <span class="highlight">match</span>ing.</span>
<span class="item"><span class="highlight">match</span></span>
Issue:
I've got a list of items. When I type into a text box, it eliminates items that do not match, and highlights the ones that do inside a /span. The issue is that if I type something that matches multiple things, I want to select the EXACT String, and not the ones that partially match (see above DOM example... the ideal match would be the 3rd one).
So, is my only hope here to go one by one until I find my perfect match, or is there something else that I've been missing?
You are trying to find a span that has no text, with an inner span that has some highlighted text. Here is an XPath selector for that:
//span[#class='item'][not(text())]/span[#class='highlight'][text()='match']
Am newbie to selenium (java).
I have to click on the menu item which is under <ul> tag as <li> items. I can able to get it with //*[#id='MainMenu']/li[5]/span xpath.
I do not want to hard code [5] of the list item, because item's position may change. It may not at 5th position all the time.
I wanted to get xpath for the particular item under particular tag with an id.
Edit:
This is how my html looks like. List item text will be loading dynamically.
<ul id="sfMainMenu" class="sf-menu ui-selectable">
<li class="ui-selected ui-selectee">
<span subnav="0" param="cmd=desk" filesrc="/Dashboard/Index"></span>
</li>
<li class="ui-selectee"></li>
<li class="ui-selectee"></li>
<li class="ui-selectee"></li>
<li class="ui-selectee">
<span subnav="18" param="cmd=desk" filesrc="../myFile.aspx"></span>
</li>
</ul>
Kindly suggest the approach with an example.
I suggest trying this:
//*[#id='MainMenu']/li[normalize-space() = 'The text you want']/span
Though if you could show us what the HTML in question actually looks like, we can provide a more reliable answer. XPath doesn't have any concept of "visual text", so if you have some text that's hidden within the li you're trying to retrieve, that could be considerably trickier.
I have solved by using filesrc attibute of span tag in the list item.
Xpath is:
//span[contains(#filesrc, 'myFile.aspx')]
As my .aspx file will be the same for any page, so I used filesrc attribute which contains the actual file name and is the only file name in that html page.
You can use the following
driver.FindElement(By.XPath("//li[contains(Text(),'Expected Text']")).Click();
Or you can avoid xpath all together by running a foreach loop on the relevant Class tag
foreach (IWebElement e in driver.FindElements(By.ClassName("ui-selectee")))
{
if (e.Displayed && e.Text == "Expected Text")
e.Click();
}