Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to select element with selenium in python and get the text?

In python I am trying to select this element

<button stid="FLIGHTS_DETAILS_AND_FARES-index-1-LDUWT-FlightsActionButton" data-test-id="select-link" data-stid="FLIGHTS_DETAILS_AND_FARES-index-1-LDUWT-FlightsActionButton" class="uitk-card-link" type="button"><span class="is-visually-hidden">Select and show fare information for Etihad Airways flight, departing at 10:50am from Geneva, arriving at 1:05pm in Tokyo, Priced at $2,224 Roundtrip per traveler. Arrives 1 day later. 19 hours 15 minutes total travel time, One stop, Layover for 3 hours 10 minutes in Abu Dhabi.</span></button>

and to extract the text. I tried to use the following expression

element = browser.find_element(By.XPATH, "//button[@stid='FLIGHTS_DETAILS_AND_FARES-index-1-LDUWT-FlightsActionButton']")
print(element.text)

I also tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

element = browser.find_element(By.XPATH, '//button[contains(text(), "Select and show fare information")]')
print(element.text)

which results in an "Unable to locate element" error.

Also the stid element might change between calls to the webpage.

>Solution :

your locators were not correct, i ahve corrected both you have provided above
try any of these below and Extract the text for the span element

#1 Since part of ID is dynamic you can just use contains

//button[contains(@stid,'FLIGHTS_DETAILS_AND_FARES')]//span

#2 The text element Tag is actually span not button

//span[contains(text(), "Select and show fare information")]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading