python使用selenium时出现的错误写法

el = driver.find_element_by_xpath('//*[@id="changeCityBox"]/ul/li[2]/a')
driver.find_element_by_xpath('//*[@id="search_input"]').send_keys('python',Keys.ENTER)

解决方法将上面写法改为

from selenium.webdriver.common.by import By
el = driver.find_element(By.XPATH,r'//*[@id="changeCityBox"]/ul/li[2]/a')
driver.find_element(By.XPATH,r'//*[@id="search_input"]').send_keys("python",Keys.ENTER)

实战这里用到的浏览器驱动为谷歌,其他浏览器也可以Edge改为edge即可,修改驱动需要配置环境

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys   # 键盘的按钮指令
from selenium.webdriver.common.by import By
driver = Chrome()
driver.get("https://www.lagou.com/")
# 找到浏览器需要操作的元素copyxpath
el = driver.find_element(By.XPATH,r'//*[@id="changeCityBox"]/ul/li[2]/a')
# el = driver.find_element_by_xpath('//*[@id="changeCityBox"]/ul/li[2]/a')
el.click()  # 点击事件
# 找到输入框的F12元素copyxpath,输入python内容,输入回车enter或者搜索按钮xpath
driver.find_element(By.XPATH,r'//*[@id="search_input"]').send_keys("python",Keys.ENTER)
# driver.find_element_by_xpath('//*[@id="search_input"]').send_keys('python',Keys.ENTER)

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐