I would like to find "How are you?" using xpath with this part of html :
<li>Hello<span class="redS bold">How are you ?</span></li
I tried with :
//span[contains(#class, 'redS bold') and text() = 'Hello']
Thanks in advance for your help
maybe
//span[contains(text(),'How are you')]
? or maybe
//span[contains(#class,'redS bold') and contains(text(),'How are you')]
Related
<p><span class="label">key</span>value</p>
How am I able to get just the "value" out using xPath? I managed to get to the element using the following expression:
//span[#class='label']/..
Try this one to get required value:
//p[span[#class='label']]/text()
You just have to use text() to get the text from the p
//span[#class='label']/../text()
I want to extract HTML inside a dic. For example in this piece of HTML:
<div id="main"><h1><xyz>Title<xyz></h1></div>
I want to extract div content: <h1><xyz>Title<xyz></h1> as a string.
Is that possible with CSS o Xpath scrapy selectors?
Thanks :)
With XPath, use the dedicated function string() :
string(//div[#id='main']/h1/xyz)
Output : "Title"
EDIT : To output the whole path if you're looking for "Title" :
concat(concat("<",name(//*[.="Title"]/parent::*),">"),concat("<",name(//*[.="Title"]),">"),string(//*[.="Title"]),concat("</",name(//*[.="Title"]),">"),concat("</",name(//*[.="Title"]/parent::*),">"))
Output : <H1><XYZ>Title</XYZ></H1>
Solution with css selector is not possible, but pretty simple with xpath:
desired_str = selector.xpath("//div[#id='main']").extract()
suppose I have this structure:
<div class="a" attribute="foo">
<div class="b">
<span>Text Example</span>
</div>
</div>
In xpath, I would like to retrieve the value of the attribute "attribute" given I have the text inside: Text Example
If I use this xpath:
.//*[#class='a']//*[text()='Text Example']
It returns the element span, but I need the div.a, because I need to get the value of the attribute through Selenium WebDriver
Hey there are lot of ways by which you can figure it out.
So lets say Text Example is given, you can identify it using this text:-
//span[text()='Text Example']/../.. --> If you know its 2 level up
OR
//span[text()='Text Example']/ancestor::div[#class='a'] --> If you don't know how many level up this `div` is
Above 2 xpaths can be used if you only want to identify the element using Text Example, if you don't want to iterate through this text. There are simple ways to identify it directly:-
//div[#class='a']
From your question itself you have mentioned the answer for it
but I need the div.a,
try this
driver.findElement(By.cssSelector("div.a")).getAttribute("attribute");
use cssSelector for best result.
or else try the following xpath
//div[contains(#class, 'a')]
If you want attribute of div.a with it's descendant span which contains text something, try as below :-
driver.findElement(By.xpath("//div[#class = 'a' and descendant::span[text() = 'Text Example']]")).getAttribute("attribute");
Hope it helps..:)
Any idea how to get /includes/images/large/BW93942/hermes-swift-retourne-kelly-32-indigo-1.jpg from here with it xpath?
/<img class="cloudzoom-gallery img-responsive cloudzoom-gallery-active" src="/includes/images/thumb/BW93942/xhermes-swift-retourne-kelly-32-indigo-1.jpg.pagespeed.ic.PQKVquZdzC.webp" data-cloudzoom="useZoom: '.cloudzoom', image: '/includes/images/BW93942/hermes-swift-retourne-kelly-32-indigo-1.jpg', zoomImage: '*/includes/images/large/BW93942/hermes-swift-retourne-kelly-32-indigo-1.jpg*' " pagespeed_url_hash="271624911" onload="pagespeed.CriticalImages.checkImageForCriticality(this);">
try Below
//img/#src
OR
//img[#class='cloudzoom-gallery img-responsive cloudzoom-gallery-active']/#src
Hope it will help you :)
You could use the XPath string function substring-before to grab the part of the #src attribute which comes before .pagespeed. When applied the the HTML you posted, the XPath
substring-before(//img[#class="cloudzoom-gallery img-responsive cloudzoom-gallery-active"]/#src, ".pagespeed")
yields
/includes/images/thumb/BW93942/xhermes-swift-retourne-kelly-32-indigo-1.jpg
I'm a little new to xpath and I was wondering if you anyone can help me understand what's wrong with the following xpath query. The server is telling me I have an "invalid predicate"
Here's the xpath:
xpath("div[span[#class='paragraphnumber]/text()='$next_pn']/#id")
I want this to find the #id of the div which contains within it a span element with the #class of "paragraphnumber" and the text which equals the number contained in the variable $next_pn. The div would look something like this:
<div id="pl8ddjkdj"><span class="paragraphnumber">3</span>lor ipsum etc etc</div>
Basically, I'm starting with the number I want to be able to find the unique id of this div.
Thanks for your help.
You have just missed a single quote (') after the name of the class paragraphnumber.
This:
xpath("div[span[#class='paragraphnumber]/text()='$next_pn']/#id")
should be
// v
xpath("div[span[#class='paragraphnumber']/text()='$next_pn']/#id")