Hi i have the following code that searches for and retrieves the first paragraph after <h3>
//div[starts-with(#id,'content_div')]/h3[.='Course Content']/following-sibling::p[1]
Because i'm using the same code on multiple web pages, some of those pages do not contain <h3> and instead contain <h4> tag. What is the syntax to check if there is a <h3> tag and if not check for <h4>?
You can check if child element exists using predicate, e.g.:
//div[span]
It will select all div elements which have span
//div[starts-with(#id,'content_div')]/*[self::h3 or self::h4 and .='Course Content']/following-sibling::p[1]
If you're using XPath 2.0 then you can replace h3 by (h3|h4).
In XPath 1.0 you'll need to replace it by *[self::h3 or self::h4]
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"
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
I need to detect via xpath an element (image with href) but i know a part of href only
This is the html
<img src="//user/banners/16/08/1614708.gif" alt="AAA" data-tip="BBB" currentitem="false" class="" width="468" height="60">
I know the id 123456 (part of href)
I tried this xpath that recognize element with a part of href but working in text link only
xpath=//a[contains(#href, "123456")]
How can i detect the element using a part of href only ?
I need xpath only please.
You need get /img in //a where href attribute contains() or ends-with() your id
This is XPATH that you need. At least i would use this XPATH in this situation
//a[ends-with(#href, 'your-id-here')]/img
You can use regular expresions. Something like
driver.find_element_by_xpath("//input[starts-with (#name,'Tut')]")
or as you described
driver.find_element_by_xpath("//input[contains(#name,'sel')]").
Be awere of one thing, do not use double quotes, for the string you are searching as atribute value. Use a single quote like I previously described.
I'm trying to search for the HTML below:
<span data-login="true" class="button-tab-links--gray hide-for-medium-only"> Olá, Visitante</span>
using
//span[#class="button-tab-links--gray hide-for-medium-only"]
at Google Chrome to search element but doesn't work. What do I need to change?
If all you're trying to find is the string "Ola Visitante", you could try search for that instead using:
//span[contains(text(), 'Ola Visitante')]
As per these three (3) information within the image:
The desired element is within an <iframe>
Solution
To search the XPath for the <span> element through google-chrome-devtools you have to select the desired <iframe> clicking on the element where currently top is selected.
I have the following HTML. I need to get the XPath using DOWN as a keyword.
<span>
"DEVICE: some random values that I'm not bothered about"
<span class="c-emoji_plain_text">:sensor_1000_4_1:</span>
"/interfaces/:/interfaces/:mib2d], TRIGGER: interface_status, MESSAGE:
$interface_name is DOWN"
</span>
The problem I'm facing here is, when I use the following XPath, it's not recognized:
//span[contains(text(),'DOWN')]
I see that the text above child span is used but not the text below it.
Kindly help.
Try to replace
//span[contains(text(),'DOWN')]
with
//span[contains(.,'DOWN')]
to select required span node
Note that such selector can match several elements. To make it more specific you can use
//span[span and contains(.,'DOWN')]
This will match span that contains span child node as well as "DOWN" substring
Also
//span[contains(text()[2],'DOWN')]
should do the trick