I'm trying to get:

letter_link = driver.find_element_by_css_selector("div.b-datalist__item__addr") 

and get the error:

 selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"div.b-datalist__item__addr"} 

Although google developer tools in the DOM "div.b-datalist__item__addr" is, but there is no element in the code of the page. I understand that html is generated by javascript. What could be the problem? How can I get a link to a letter to click on it?

  • See how the driver page sees before you access the element. I am sure that the same element has a different class, or it simply does not exist yet. - Narnik Gamarnik
  • @NarnikGamarnik, thanks for the tip! I'm just starting to learn selenium and its driver. I understand the driver sees the script, and not the result of its work, i.e. can we say to the driver, what would he wait for the scripts to generate html and then access the class? - Imran Mekerov
  • In general, I used Selenium together with BeautifulSoup, and parsil stores, where the interface on React.js Perhaps the method that I used is not the best or not at all correct, but I managed to get the result. - Narnik Gamarnik
  • I found my old parser, specially made a gist, where I put the code. Initially, this parser was built into the application on Flask, so there are extra imports there. Immediately pay attention to the def function get_html (url), and call driver.quit () at the end. gist.github.com/narnikgamarnikus/… - Narnik Gamarnik
  • @NarnikGamarnik, thanks. As I thought, it was necessary to wait until the javascript generates the page. For some reason, time.sleep (seconds) did not work. In the documentation for selenium, this method of explicit waiting is critical and suggests using WebDriverWait from selenium.webdriver.support.ui. What in my case solved the problem. - Imran Mekerov

1 answer 1

Link to an article on Habré about expectations.

 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 driver = webdriver.Firefox() driver.get("http://somedomain/url_that_delays_loading") element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.js-href.b-datalist__item__link"))) element.click()