How to extract the text from a child node which is within a <div> tag through Selenium and WebDriver? - selenium

I need to get the value 107801307 that is inside a specific Div but there are several other Divs in the path before getting into that DIV I need. Can anyone help?
Below is the image with the information that I need to extract from the DIV.

As per the HTML you have provided, to extract the text 107801307 you can use the following solution:
Java:
String myText = driver.findElement(By.xpath("//b[#class='label_tratamento'][contains(.,'Ban Claro')]//following::span[1]").getAttribute("innerHTML");
Python:
myText = driver.find_element_by_xpath("//b[#class='label_tratamento'][contains(.,'Ban Claro')]//following::span[1]").get_attribute("innerHTML")

Research xpath locators to find the specific element you want.
Assuming you were using Java, the code would be:
webdriver.findElement(By.xpath("//b[text()='Ban Claro']/following::span").getText();
or
webdriver.findElement(By.xpath("//b[#class='label_tratamento']/following::span").getText();

Use:
driver.findElement(By.xpath(("XPATH OF DIV HERE")[Index of the div where span is. Example: 4)/span)).getText();

Related

Selenium XPath: how to get href value of a use attribute

I am trying to get the xpath of a svg that has an attribute <use href= "#icon-map">
So far the path //*[local-name()='svg']/*[local-name()='use'] works, but it finds 84 entries.
How can I modify the xpath in order to select only the use that has the href as "#icon-map"?
You can use this:
//*[local-name()='svg'][use[#href="#icon-map"]]
or
//*[local-name()='svg'][*[local-name()='use'][#href="#icon-map"]]
See example.
If you have more results than you expect then you should use more specific paths to the element or take your query into (..) and add number of an item into [..] like :
(//*[local-name()='svg'][use[#href="#icon-map"]])[2]
If use is an attribute then you could do this :
//*[name()='svg']//*[#use and #href='#icon-map']
Also the above solution assumes that #icon-map is unique in HTML DOM

How to search the span element through XPath within google-chrome-devtools

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.

Unable to locate an element using text attribute in XPath

I am trying to locate element using relative XPath. I have attached HTML schema of the element. Below is the XPath I am using:
//a[contains(text(),"Sales")].
If you want to locate link by text you might need to use search by link text instead of XPath:
Java:
driver.findElement(By.linkText("Sales")).click();
Python:
driver.find_element_by_link_text("Sales").click()
Note that you should use exact value as it appears on rendered page in browser:
if it appears as SALES:
driver.find_element_by_link_text("SALES")
if it appears as "Sales":
driver.find_element_by_link_text("\"Sales\"")
In case some extra text is added by ::before pseudo-element, you can also use search by partial link text:
driver.find_element_by_partial_link_text("Sales")
//*[#class='**your class name**']//*[text()='Sales']
Hope above XPath helps you!

Not able to find Xpath for Parent Child relation in Div tag

Hi
I am not able to find the correct xpath for the Html code added in the attachment.
I have written below xpath but it is not working.
//div[#class="calendar-item-title"][1]//*[#class="calendar-item-time"][1]
Please find attached image for more details of Html code.
div[#class="calendar-item-title"] does not have a child *[#class="calendar-item-time"] according to your image.
Your *[#class="calendar-item-time"] is in div[#class="items-container"]
so your xpath would be:
//div[#class="items-container"][1]//a[#class="calendar-item-time"][1]
Just add : "/parent::*" at the end of //div[#class="calendar-item-title"] if you want the parent element
Try this xpath :
//a[contains(text(),'12:30 am')]
try with the below xpath
driver.findElement(By.xpath("//a[text()='12:30 am']")).click()
from ur image, its not clear there are more 12.30 containing text or not. In that case, use
driver.findElements(By.xpath("//a[text()='12:30 am']")).get(0).click();

how to locate element with selenium webdriver for below html

I have an issue clicking on the below HTML:
<div id="P7d2205a39cb24114b60b80b3c14cc45b_1_26iT0C0x0" style="word-wrap:break-word;white-space:pre-wrap;font-weight:500;" class="Ab73b430b430a49ebb0a0e8a49c8d71af3"><a tabindex="1" style="cursor:pointer;" onclick="var rp=$get('ctl00_ContentPlaceHolder1_ReportViewer1_ctl10_ReportControl');if(rp&&rp.control)rp.control.InvokeReportAction('Toggle','26iT0C0x0');return false;" onkeypress="if(event.keyCode == 13 || event.which == 13){var rp=$get('ctl00_ContentPlaceHolder1_ReportViewer1_ctl10_ReportControl');if(rp&&rp.control)rp.control.InvokeReportAction('Toggle','26iT0C0x0');}return false;"><img border="0" src="/Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=10.0.30319.1&Name=Microsoft.ReportingServices.Rendering.HtmlRenderer.RendererResources.TogglePlus.gif" alt="+"></a> 2013</div>
I have used the below script to click anchor inside a div tag. For the above html code it is not fixed only end part of id example "26iT0C0x0" is fixed. The script that I have used is:
WebElement e1=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[ends-with(#id,'26iT0C0x0')]/a")));
e1.click();
You can use the 'contains' method within an xpath lookup:
driver.findElement(By.xpath("//div[contains(#id,'26iT0C0x0')]")
I would recommend you to consider CSS selector alternative as CSS working faster, than xpath.
So 'contains' in attribute in CSS stands for '*=', for example
if we want to find attribute by 'CSS' ending in this: <htmlTag A="blablaCSS" > we need do the following:
String CSSselector="htmlTag[A*=CSS]";
and you get this element searched.
So considering your example CSS selector be like:
String cssSearched="div[id*=26iT0C0x0] a";
also try to click not on link - a
but on parent div as well:
String cssSearched="div[id*=26iT0C0x0]";
driver.findElement(By.cssSelector(cssSearched));
hope this works for you.
As Mark Rwolands already mentioned: the xpath-Function 'ends-with()' isn't supported in Selenium 2.
Also, if you maybe consider to use chromeDriver in the future, I would recommend clicking the image, not the anchor, see:
https://sites.google.com/a/chromium.org/chromedriver/help/clicking-issues
edit:
Also your IDs are looking generated. I wouldn't count on them for a stable test-environment.