1

I am new to Selenium and Python. I would like to navigate through a website, find an element and print it (or store it in a csv file).

Python version: 3.10; Selenium Webdriver: Firefox; IDE: PyCharm 2021.3.2 (CE); OS: Fedora 35 VM

So far I am able to navigate to the appropriate page where a table is generated. When I locate the element by ID and attempt to print it, the output printed is not the element I see on the screen.

My relevant code:

RemainDue = driver.find_element(By.ID, 'b8-b36-Input_RemainAmtYr1')
print ('Remaining Due:', RemainDue)

I expect the output to be something like "100.50", which is what I see on the screen. Instead I get the following:

Remaining Due: <selenium.webdriver.remote.WebElement (session="c33b682a-faa3-4109-8f53-60842fabbbc9", element="32a34d01-66e5-4b98-9577-fab4ca21f988")>

What am I doing wrong?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Auditor
  • 45
  • 1
  • 10
  • Please don't change the question based on which you have received well researched answers. Once you receive canonical answers changing the question can make all the existing answers invalid and may not be useful to future readers. If your requirement have changed feel free to raise a new question. StackOverflow contributors will be happy to help you out. For the time being I have reverted back the question to it's initial state. – undetected Selenium Mar 11 '22 at 13:09

1 Answers1

1

You are printing the WebElement. Hence you see the output as:

Remaining Due: <selenium.webdriver.remote.WebElement (session="c33b682a-faa3-4109-8f53-60842fabbbc9", element="32a34d01-66e5-4b98-9577-fab4ca21f988")>

You may like to print the text within the element using the text attribute as follows:

RemainDue = driver.find_element(By.ID, 'b8-b36-Input_RemainAmtYr1')
print ('Remaining Due:', RemainDue.text)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you. I changed RemanDue to RemainDue.text as you indicated above. The output is now "Remaining Due:" with nothing at all after it. – Auditor Mar 11 '22 at 12:40
  • @Auditor Thats because the [WebElement](https://stackoverflow.com/a/52805139/7429447) which you have identified using the [locator strategy](https://stackoverflow.com/a/48376890/7429447) doesn't have any text. That's why no text is printed out. – undetected Selenium Mar 11 '22 at 12:43
  • I suspected as much. I posted the HTML code of the element I am trying to locate above. I hovered over the code that includes value="$100.50" and Copied the CSS Selector. I changed my code to locate the element of the HTML that includes $100.50 by CSS Selector. I still get the same output. – Auditor Mar 11 '22 at 13:06
  • @Auditor That should be a different question all together. Feel free to raise a new question with your new requirement. – undetected Selenium Mar 11 '22 at 13:08
  • Good point. Will do. Thanks again. – Auditor Mar 11 '22 at 13:10