Just started to study selenium, tell me how can I find the following element on the page: <div class="im-mess--text wall_module _im_log_body"> and get its contents as text?

div class

  • Do not forget to mark the answer, if they helped you - AtachiShadow

2 answers 2

text = driver.find_element_by_xpath(DIV_XPATH).text , where DIV_XPATH = '//div[@class="im-mess--text wall_module _im_log_body"]' or DIV_XPATH = '//div[contains(@class, "wall_module")]' (if not all the classes or classes are important for the element change)

  • I do not understand what I am doing wrong, but at the output I get an empty string in both cases - Clark Devlin
  • @ClarkDevlin, you can at least link to the page where this div? - ChocolateSwan
  • div from any dialogue in VK where there is a decent number of messages, i.e., vk.com/im?sel=id - Clark Devlin
  • @ClarkDevlin, I can assume that the desired item does not have time to load at the time of the search, you can use WebDriverWait(driver, 10, 0.05).until(func) - ChocolateSwan to wait for the item to load.
  • one
    @ClarkDevlin, most likely this is because there are many such elements on the page, replace find_element_by_xpath with find_elements_by_xpath (like so) - ChocolateSwan

Try this:

 div_text = driver.find_element_by_css_selector("div.im-mess--text.wall_module._im_log_body").text 
  • Well, I do not give out any errors, however, when I want to output it, that is, print(div_text) , I get nothing, just an empty line - Clark Devlin