Home > Software engineering >  Use selenium for find element
Use selenium for find element

Time:11-15

I would like to take the word "buy"

browser.find_element_by_xpath("//*[@id='js-commentaire']")

print(commentaire)

and i also did

browser.find_element_by_id("js-commentaire")

print(commentaire)

This the source code

"div  id="js-commentaire"> buy</div"

CodePudding user response:

You will need following libs:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

And then try this:

my_element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@id='js-commentaire']")))
print(my_element.text)

Or

my_element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@id='js-commentaire']")))
print(my_element.get_attribute('textContent'))

CodePudding user response:

In order to have the possibility to print the result of the browser.find_element() function you need to store its result into a variable like that:

commentaire = browser.find_element_by_xpath("//*[@id='js-commentaire']")

and then you'll be able to print it with

print(commentaire)

But this will print just the object and will be useless. To print the content of the element that's what you need to do:

print(commentaire.text)
  • Related