Home > OS >  AttributeError: 'WebDriver' object has no attribute 'find_elemen_by_css_selector'
AttributeError: 'WebDriver' object has no attribute 'find_elemen_by_css_selector'

Time:09-16

I get this error:AttributeError: 'WebDriver' object has no attribute 'find_elemen_by_css_selector'

What is the current usage?

from selenium import webdriver


from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()

url = "http://github.com/"
driver.get(url)

search_input = driver.find_element("name", "q")

time.sleep(1)
search_input.send_keys("python")
time.sleep(3)
search_input.send_keys(Keys.ENTER)
time.sleep(1)
result = driver.find_elemen_by_css_selector(".repo-list-item h3 a")



for element in result:
    print(element)


driver.close()

CodePudding user response:

Looks lire tou forgot a t

driver.find_element_by_css_selector(".repo-list-item h3 a")

CodePudding user response:

You have missing t in the below line at elemen

result = driver.find_elemen_by_css_selector(".repo-list-item h3 a")

so the correct line is :

result = driver.find_element_by_css_selector(".repo-list-item h3 a")

CodePudding user response:

You can do this:

from selenium.webdriver.common.by import By

result = driver.find_element(By.CSS_SELECTOR, '.repo-list-item h3 a')

CodePudding user response:

So, here it is.
I am not sure what you're trying to achieve with this code, I printed the href of every result.
Well, you can try the code, understand it and modify it as you like.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

driver.get("http://github.com/")
search_input = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//input[@name='q']")))
search_input.send_keys("python")
search_input.send_keys(Keys.ENTER)

result = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.XPATH, "//a[@class='v-align-middle']")))
for element in result:
    print(element.get_attribute("href"))

driver.close()

Note that I've used explicit waits instead of the less performant time.sleep().
I also personally prefer to locate elements by XPath but of course you can do it by CSS selector if you want.

If it helped please mark the answer as accepted :)

  • Related