1

How can I get the text value "This Text 1" and "This Text 2" with selemiun and python from the following html code:

<p>
   <b>Studio:</b>
   <a href="http://www.somelink.com/some_page/">This Text 1</a>
   " | "
   <b>Director:</b> This Text 2
</p>

I have tried in several ways like with this:

driver.find_element_by_xpath("//*[contains(text(), 'Director:')]")

but without get the result.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
AlexGiov
  • 25
  • 1
  • 4
  • Possible duplicate of [How to get text with selenium web driver in python](https://stackoverflow.com/questions/20996392/how-to-get-text-with-selenium-web-driver-in-python) – trotta Jul 11 '19 at 08:11

1 Answers1

0

To get the text This Text 1 and This Text 2 Use following options.

To get This Text 1 Use following xpath

print(driver.find_element_by_xpath("//p/b[contains(.,'Studio')]/following::a").text)

Use JavaScript Executor to fetch This Text 2

element=driver.execute_script('return arguments[0].lastChild.textContent;', driver.find_element_by_xpath("//p[contains(.,'Director')]"))
print(element.strip())
KunduK
  • 32,888
  • 5
  • 17
  • 41