我正试图在谷歌搜索栏上写一个搜索。
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:\Users\HP\PycharmProjects\SeleniumTest1\Drivers\chromedriver.exe")
driver.get("https://www.google.com/")
driver.maximize_window()
driver.find_element_by_name("q").send_keys("LinkedIn login")
driver.find_element_by_name("q").send_keys(Keys.ENTER)
driver.find_element_by_partial_link_text("LinkedIn Login").click()
driver.find_element_by_id("username").send_keys("enter your username")
driver.find_element_by_id("password").send_keys("enter your password")
driver.find_element_by_tag_name("button").click()
time.sleep(5)
print(driver.title)
print(driver.current_url)
driver.close()
这是我从控制台得到的错误
driver.find_element_by_name("q").send_keys("LinkedIn login")
AttributeError: 'NoneType' object has no attribute 'send_keys'
当前的selenium版本使用find_element()
来获取元素。请尝试下面的代码。将by属性更改为您正在使用搜索的属性
driver.find_element(by="name", value = "q").send_keys("LinkedIn login")