Kakashi - Sensei
Kakashi - Sensei

Reputation: 381

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

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.

Div

Upvotes: 1

Views: 158

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193348

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")
    

Upvotes: 1

user9900402
user9900402

Reputation: 11

Use:

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

Upvotes: 1

TomH
TomH

Reputation: 2729

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();

Upvotes: 1

Related Questions