Selenium“Options”对象没有属性“find_element”

6vl6ewon  于 2023-04-12  发布在  其他
关注(0)|答案(2)|浏览(193)

我打开一个网站,upwork.com,做一些网页抓取,但试图点击一个元素是抛出一个错误。
验证码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options: Options = webdriver.ChromeOptions()
options. add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
driver.get ("https://www.upwork.com/nx/jobs/search/?from_recent_search=true&q=webscraping&sort=recency")
options.find_element("xpath", '//*[@id="job-1645717982262001664"]/div[1]/div/h3/a').click()

错误:

Traceback (most recent call last):
  File "/Users/yatharthmahajan/Documents/Python/pythonProject/webscraper.py", line 8, in <module>
    options.find_element("xpath", '//*[@id="job-1645717982262001664"]/div[1]/div/h3/a').click()
AttributeError: 'Options' object has no attribute 'find_element'
nhn9ugyo

nhn9ugyo1#

您正在options对象上使用find_element方法,这是不正确的。您应该像下面这样在驱动程序对象上使用find_element方法。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)

driver.get("https://www.upwork.com/nx/jobs/search/?from_recent_search=true&q=webscraping&sort=recency")
link = driver.find_element_by_xpath('//*[@id="job-1645717982262001664"]/div[1]/div/h3/a')
link.click()
wdebmtf2

wdebmtf22#

它应该是:

driver.find_element("xpath", '//*[@id="job-1645717982262001664"]/div[1]/div/h3/a').click()

不包括:

options.find_element("xpath", '//*[@id="job-1645717982262001664"]/div[1]/div/h3/a').click()

相关问题