Selenium:Python脚本无法找到’service’参数

huangapple go评论97阅读模式
英文:

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=&quot;xy.com&quot;,
user=&quot;dddddd&quot;,
password=&quot;xxxxxxx&quot;,
database=&quot;xxxxx&quot;
)

service_object = Service(&#39;./chromedriver&#39;)

driver = webdriver.Chrome(service=service_object)

driver.get(&#39;https://www.xxxxxx.com/&#39;)

sleep(5)

article_element = driver.find_element(By.CLASS_NAME, &#39;line-name &#39;)

article_text = article_element.text

sql = &quot;REPLACE INTO dfbase (name) VALUES (%s)&quot;
val = [(article_text)]
cursor = mydb.cursor()
cursor.execute(sql, val)
print(&quot;Inserted&quot;,cursor.rowcount,&quot;row(s) of data.&quot;)
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 &quot;automation.py&quot;, line 19, in &lt;module&gt;
driver = webdriver.Chrome(service=service_object)
TypeError: __init__() got an unexpected keyword argument &#39;service&#39;

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

huangapple
  • 本文由 发表于 2023年6月8日 08:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定