PyCharm 中的 Selenium 问题——一切似乎都已正确更新,但无法使用方法

问题描述 投票:0回答:1

我在运行 Chrome 的 Mac Mini 上:版本 115.0.5790.114(官方版本)(arm64),刚刚下载并安装了我在此处找到的 Chrome 驱动程序:https://googlechromelabs.github.io/chrome-for-测试/ - 稳定版本 - Mac-Arm64。

我进去安装并升级了selenium和pip,并使用这个终端命令使chromedriver可执行:chmod +x /usr/local/bin/chromedriver。

我尝试了此代码的两个版本,以尝试从网站提取并打印价格元素。

**版本 1:一个版本拉出了网站,但没有打印任何内容。我收到的错误消息表明

the WebDriver object has no attribute known as "find_element_by_id" 

但显然这仍然是在 selenium 内部运行的方法?

from selenium import webdriver # Importing necessary modules from Selenium
from selenium.webdriver.chrome.options import Options # Importing the Options class from selenium.webdriver.chrome.options

chrome_driver_path = "/Users/myusername/Documents/pythonProject1/chromedriver"

options = Options()
options.executable_path = chrome_driver_path

driver = webdriver.Chrome(options=options)

driver.get("https://www.amazon.com")
chrome_driver_path = "/Users/myusername/Documents/pythonProject1/chromedriver"
driver = webdriver.Chrome(options=options)

driver.get("https://www.amazon.com/Coleshome-Computer-Writing-Workstation-Vintage/dp/B0B6V83D3Q/?_encoding=UTF8&pd_rd_w=dhBqd&content-id=amzn1.sym.5f7e0a27-49c0-47d3-80b2-fd9271d863ca%3Aamzn1.symc.e5c80209-769f-4ade-a325-2eaec14b8e0e&pf_rd_p=5f7e0a27-49c0-47d3-80b2-fd9271d863ca&pf_rd_r=TAX9FJM5MT6DWYCYYMRD&pd_rd_wg=b356N&pd_rd_r=b79c530e-5b03-411d-8957-0ec821732764&ref_=pd_gw_ci_mcx_mr_hp_atf_m")
price = driver.find_element_by_id("priceblock_ourprice")
print(price)

**版本 2:不打印任何内容或访问网站并返回错误:

'driver = webdriver.Chrome(executable_path=user_path, options=options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'" 

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

user_path = "/Users/user1/Documents/pythonProject1/chromedriver"

options = Options()

driver = webdriver.Chrome(executable_path=user_path, options=options)
driver.get("https://www.amazon.com/Coleshome-Computer-Writing-Workstation-Vintage/dp/B0B6V83D3Q/?_encoding=UTF8&pd_rd_w=dhBqd&content-id=amzn1.sym.5f7e0a27-49c0-47d3-80b2-fd9271d863ca%3Aamzn1.symc.e5c80209-769f-4ade-a325-2eaec14b8e0e&pf_rd_p=5f7e0a27-49c0-47d3-80b2-fd9271d863ca&pf_rd_r=TAX9FJM5MT6DWYCYYMRD&pd_rd_wg=b356N&pd_rd_r=b79c530e-5b03-411d-8957-0ec821732764&ref_=pd_gw_ci_mcx_mr_hp_atf_m")
price = driver.find_element_by_id("priceblock_ourprice")
print(price.text)
driver.quit()

我希望能够从亚马逊网站接收价格元素,然后在执行此代码后将其打印出来。谢谢。

python pycharm selenium-chromedriver
1个回答
0
投票

尝试硒 4

pip install webdriver-manager==4.0.0
pip install selenium==4.10.0

代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver_path = ChromeDriverManager().install()
print(f"=> Driver Path is: {driver_path}")
s = Service(driver_path)
driver = webdriver.Chrome(service=s)

driver.get("https://www.amazon.com/Coleshome-Computer-Writing-Workstation-Vintage/dp/B0B6V83D3Q/?_encoding=UTF8&pd_rd_w=dhBqd&content-id=amzn1.sym.5f7e0a27-49c0-47d3-80b2-fd9271d863ca%3Aamzn1.symc.e5c80209-769f-4ade-a325-2eaec14b8e0e&pf_rd_p=5f7e0a27-49c0-47d3-80b2-fd9271d863ca&pf_rd_r=TAX9FJM5MT6DWYCYYMRD&pd_rd_wg=b356N&pd_rd_r=b79c530e-5b03-411d-8957-0ec821732764&ref_=pd_gw_ci_mcx_mr_hp_atf_m")

price = driver.find_element(By.XPATH, '//*[@data-cel-widget="corePriceDisplay_desktop_feature_div"]//span[@class="a-price-whole"]').text
print(price)
driver.quit()

输出:

© www.soinside.com 2019 - 2024. All rights reserved.