英文:
Selenium: Python script can't find 'service' argument
问题
我有一个Python脚本,结合selenium-webdriver来爬取特定网站并提取类的内容。然后,我将结果保存在MySQL数据库中。
文件(automation.py)位于服务器目录/www/htdocs/xxx/wow/test:
import mysql.connector
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
mydb = mysql.connector.connect(
host="xy.com",
user="dddddd",
password="xxxxxxx",
database="xxxxx"
)
service_object = Service('./chromedriver')
driver = webdriver.Chrome(service=service_object)
driver.get('https://www.xxxxxx.com/')
sleep(5)
article_element = driver.find_element(By.CLASS_NAME, 'line-name ')
article_text = article_element.text
sql = "REPLACE INTO dfbase (name) VALUES (%s)"
val = [(article_text)]
cursor = mydb.cursor()
cursor.execute(sql, val)
print("Inserted", cursor.rowcount, "row(s) of data.")
mydb.commit()
mydb.close()
driver.quit()
在我的本地环境中,脚本按预期运行,但当我在Web服务器上使用它时,出现以下错误:
ssh-xxxx@xxxx:/www/htdocs/xxx/wow/test$ python automation.py
Traceback (most recent call last):
File "automation.py", line 19, in <module>
driver = webdriver.Chrome(service=service_object)
TypeError: __init__() got an unexpected keyword argument 'service'
"pip list"的输出:
ssh-xxxx@xxxx:/www/htdocs/xxx/wow/test$ pip list
Package Version
---------------------- --------
apparmor 2.12
beautifulsoup4 4.6.0
bs4 0.0.1
certifi 2023.5.7
charset-normalizer 2.0.12
chromedriver-installer 0.0.6
idna 3.4
LibAppArmor 2.12
lxml 4.9.2
matplotlib 2.1.1
numpy 1.13.3
pexpect 4.2.1
pip 21.3.1
python-dotenv 0.20.0
requests 2.27.1
selenium 3.141.0
setuptools 59.6.0
urllib3 1.26.16
webdriver-manager 3.7.1
wheel 0.37.1
英文:
I've a Python script which I combine with the selenium-webdriver to crawl a certain website and extract the content of a class. After that I save the result inside a MySQL database.
The file (automation.py) is located in the server directory /www/htdocs/xxx/wow/test:
import mysql.connector
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
mydb = mysql.connector.connect(
host="xy.com",
user="dddddd",
password="xxxxxxx",
database="xxxxx"
)
service_object = Service('./chromedriver')
driver = webdriver.Chrome(service=service_object)
driver.get('https://www.xxxxxx.com/')
sleep(5)
article_element = driver.find_element(By.CLASS_NAME, 'line-name ')
article_text = article_element.text
sql = "REPLACE INTO dfbase (name) VALUES (%s)"
val = [(article_text)]
cursor = mydb.cursor()
cursor.execute(sql, val)
print("Inserted",cursor.rowcount,"row(s) of data.")
mydb.commit()
mydb.close()
driver.quit()
On my local environment the script is working as expected, but when I use it on my webserver I get the following error:
ssh-xxxx@xxxx:/www/htdocs/xxx/wow/test$ python automation.py
Traceback (most recent call last):
File "automation.py", line 19, in <module>
driver = webdriver.Chrome(service=service_object)
TypeError: __init__() got an unexpected keyword argument 'service'
Outout of "pip list":
ssh-xxxx@xxxx:/www/htdocs/xxx/wow/test$ pip list
Package Version
---------------------- --------
apparmor 2.12
beautifulsoup4 4.6.0
bs4 0.0.1
certifi 2023.5.7
charset-normalizer 2.0.12
chromedriver-installer 0.0.6
idna 3.4
LibAppArmor 2.12
lxml 4.9.2
matplotlib 2.1.1
numpy 1.13.3
pexpect 4.2.1
pip 21.3.1
python-dotenv 0.20.0
requests 2.27.1
selenium 3.141.0
setuptools 59.6.0
urllib3 1.26.16
webdriver-manager 3.7.1
wheel 0.37.1
答案1
得分: 1
因为您的网络服务器使用了一个非常旧的selenium
版本(3.141.0
)。在那个版本中不存在service
参数:https://github.com/SeleniumHQ/selenium/blob/selenium-3.141.0/py/selenium/webdriver/chrome/webdriver.py#L33
您可能希望在本地环境和服务器环境之间比较pip list
,因为您可能在本地环境中使用的是selenium
版本4
,这就是为什么service
参数存在的原因:https://github.com/SeleniumHQ/selenium/blob/selenium-4.10.0/py/selenium/webdriver/chrome/webdriver.py#L32
英文:
It's because your web server is using a very old version of selenium
(3.141.0
). The service
arg doesn't exist there: https://github.com/SeleniumHQ/selenium/blob/selenium-3.141.0/py/selenium/webdriver/chrome/webdriver.py#L33
You may want to compare pip list
between your local environment and your server environment, as you are likely using selenium
4
on your local environment, which is why service
exists there: https://github.com/SeleniumHQ/selenium/blob/selenium-4.10.0/py/selenium/webdriver/chrome/webdriver.py#L32
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论