“没有名为 'selenium.webdriver.common.by' 的模块”Selenium Python 中的错误
'No module named 'selenium.webdriver.common.by'' Error in Selenium Python
我在尝试 运行 此代码时在 Sublime Text 中得到此输出:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:/Users/josh.bailey/Documents/chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get ("https://vehicleenquiry.service.gov.uk/")
time.sleep(5)
search = driver.find_element_by_id("wizard_vehicle_enquiry_capture_vrn_vrn")
search.send_keys("*REDACTED*")
search.send_keys(Keys.RETURN)
time.sleep(5)
main = driver.find_element_by_class("reg-mark-sm")
print(main.text)
driver.quit()
这是非常基本的代码,我只是想尝试一些方法,以便在将车辆详细信息输入到我们的数据库时简化业务流程。代码还没有完成,但我只是因为这个错误打印输出而卡住了:
回溯(最近调用最后):
File "C:\Users\josh.bailey\Documents\Test3.py", line 2, in <module>
from selenium import webdriver
File "C:\Users\josh.bailey\AppData\Roaming\Python\Python38\site-
packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:\Users\josh.bailey\AppData\Roaming\Python\Python38\site-
packages\selenium\webdriver\firefox\webdriver.py", line 27, in
<module>
from selenium.webdriver.remote.webdriver import WebDriver as
RemoteWebDriver
File "C:\Users\josh.bailey\AppData\Roaming\Python\Python38\site-
packages\selenium\webdriver\remote\webdriver.py", line 41, in
<module>
from .shadowroot import ShadowRoot
File "C:\Users\josh.bailey\AppData\Roaming\Python\Python38\site-
packages\selenium\webdriver\remote\shadowroot.py", line 21, in
<module>
from ..common.by import By
ModuleNotFoundError: No module named 'selenium.webdriver.common.by'
[Finished in 195ms]
在selenium\webdriver\common目录下肯定有一个by.py文件,就像有一个keys.py文件一样。我打开了 by.py 文件,它似乎包含了它应该包含的所有内容。我做错了什么吗?任何帮助,将不胜感激。谢谢!
您好,请更正该行 "find_element_by_class_name"
main = driver.find_element_by_class("reg-mark-sm")
to
main = driver.find_element_by_class_name("reg-mark-sm")
这里是完整的代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:/Users/josh.bailey/Documents/chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get ("https://vehicleenquiry.service.gov.uk/")
time.sleep(5)
search = driver.find_element_by_id("wizard_vehicle_enquiry_capture_vrn_vrn")
search.send_keys("*REDACTED*")
search.send_keys(Keys.RETURN)
time.sleep(5)
main = driver.find_element_by_class_name("reg-mark-sm")
print(main.text)
driver.quit()
我在尝试 运行 此代码时在 Sublime Text 中得到此输出:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:/Users/josh.bailey/Documents/chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get ("https://vehicleenquiry.service.gov.uk/")
time.sleep(5)
search = driver.find_element_by_id("wizard_vehicle_enquiry_capture_vrn_vrn")
search.send_keys("*REDACTED*")
search.send_keys(Keys.RETURN)
time.sleep(5)
main = driver.find_element_by_class("reg-mark-sm")
print(main.text)
driver.quit()
这是非常基本的代码,我只是想尝试一些方法,以便在将车辆详细信息输入到我们的数据库时简化业务流程。代码还没有完成,但我只是因为这个错误打印输出而卡住了:
回溯(最近调用最后):
File "C:\Users\josh.bailey\Documents\Test3.py", line 2, in <module>
from selenium import webdriver
File "C:\Users\josh.bailey\AppData\Roaming\Python\Python38\site-
packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:\Users\josh.bailey\AppData\Roaming\Python\Python38\site-
packages\selenium\webdriver\firefox\webdriver.py", line 27, in
<module>
from selenium.webdriver.remote.webdriver import WebDriver as
RemoteWebDriver
File "C:\Users\josh.bailey\AppData\Roaming\Python\Python38\site-
packages\selenium\webdriver\remote\webdriver.py", line 41, in
<module>
from .shadowroot import ShadowRoot
File "C:\Users\josh.bailey\AppData\Roaming\Python\Python38\site-
packages\selenium\webdriver\remote\shadowroot.py", line 21, in
<module>
from ..common.by import By
ModuleNotFoundError: No module named 'selenium.webdriver.common.by'
[Finished in 195ms]
在selenium\webdriver\common目录下肯定有一个by.py文件,就像有一个keys.py文件一样。我打开了 by.py 文件,它似乎包含了它应该包含的所有内容。我做错了什么吗?任何帮助,将不胜感激。谢谢!
您好,请更正该行 "find_element_by_class_name"
main = driver.find_element_by_class("reg-mark-sm")
to
main = driver.find_element_by_class_name("reg-mark-sm")
这里是完整的代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:/Users/josh.bailey/Documents/chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get ("https://vehicleenquiry.service.gov.uk/")
time.sleep(5)
search = driver.find_element_by_id("wizard_vehicle_enquiry_capture_vrn_vrn")
search.send_keys("*REDACTED*")
search.send_keys(Keys.RETURN)
time.sleep(5)
main = driver.find_element_by_class_name("reg-mark-sm")
print(main.text)
driver.quit()