1

Let's say I'm in a page and it has a span class called "someId", how do I search for this and get the Id that's in here.

<span class="someId">what is in here</span>

for example

<span class="someId">123456</span>

I want something that does this

element = driver.find_element('someId')
print(element) -> 123456
teemo timmy
  • 37
  • 2
  • 4

2 Answers2

0

As per the HTML you have shared I strongly believe the Locator Strategy find_element_by_xpath("span[@class='someId']") is too broad to identify an element uniquely. You may require to take help of a Locator Strategy which identifies the WebElement uniquely. For that you have to traverse the HTML DOM a bit up to construct a Locator which uniquely identifies the element.

With the current HTML you have shared, to search for the text and print it you can use the following line of code :

print(driver.find_element_by_xpath("span[@class='someId']").get_attribute("innerHTML"))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can also try with text mehtod which will return text with in that webelment

print(driver.find_element_by_xpath("span[@class='someId']").text)
Ankur Singh
  • 1,239
  • 1
  • 8
  • 18