Home > Blockchain >  Getting text from a div code object with Selenium
Getting text from a div code object with Selenium

Time:01-31

I'm trying to get the text inside these div's but I'm not succeeding. They have a code between the class that doesn't change with each execution.

<div data-v-a5b90146="" > Text to be captured</div>
            

<div data-v-a5b90146="" ><b> TEXT WANTED </b><div><br></div>

I've tried with XPATH, but I was not successful too.

content = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '/html/body/div/div/div/div/div/div/div[1]/div[2]/div[2]/div[4]/div/div/b'))).text

CodePudding user response:

You need to change couple of things.

  1. presence_of_all_elements_located() returns list of elements, so you can't use .text with a list. To get the text value of the element you need to iterate and then get the text.

  2. xpath seems very fragile. you should use relative xpath since class name is unique you can use the classname.

your code should be like.

contents = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//div[@][@data-v-a5b90146]')))
for content in contents:
   print(content.text)

You can use visibility_of_all_elements_located() as well

 contents = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, '//div[@][@data-v-a5b90146]')))
 for content in contents:
     print(content.text)

CodePudding user response:

Both the <div> tags have the attribute


Solution

To extract the texts from the <div> tags instead of presence_of_all_elements_located(), you have to induce WebDriverWait for visibility_of_all_elements_located() and using list comprehension and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.html-content")))])
    
  • Using XPATH:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='html-content']")))])
    
  • Note : You have to add the following imports :

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

CodePudding user response:

Try to get the element first then get the text from the element;

    element = WebDriverWait(
    driver,
    10
).until(
    EC.presence_of_all_elements_located((
        By.XPATH, '/html/body/div/div/div/div/div/div/div[1]/div[2]/div[2]/div[4]/div/div/b'
    ))
)
content = element.text
  • Related