Python selenium get()方法不起作用


from selenium import webdriver
import webbrowser
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
import smtplib
import sys

option = webdriver.ChromeOptions()
option.add_argument("--start-maximized")
chrome_path = "C:Program FilesGoogleChromeApplicationchrome.exe"
driver = webdriver.Chrome(chrome_path, options=option)
driver.get('https://play.typeracer.com/')
while True:
try:
driver.find_element_by_xpath("""//*[@id="dUI"]/table/tbody/tr[2]/td[2]/div/div[1]/div/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[1]/td/a""").click()
break
except:
continue
sleep(5)
try:
words = driver.find_element_by_xpath("""//*[@id="gwt-uid-15"]/table/tbody/tr[2]/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/div/div/span[1]""").text
except:
print("")
try:
words += driver.find_element_by_xpath("""//*[@id="gwt-uid-15"]/table/tbody/tr[2]/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/div/div/span[2]""").text + " "
except:
print("")
try:
words += driver.find_element_by_xpath("""//*[@id="gwt-uid-15"]/table/tbody/tr[2]/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/div/div/span[3]""").text
except:
print("")

print(words)

inputField = driver.find_element_by_xpath("""//*[@id="gwt-uid-15"]/table/tbody/tr[2]/td/table/tbody/tr[2]/td/input""")
#Sleeps because the game can detect cheaters if the sleep is not used...Slows down the WPM slightly
for character in words:
inputField.send_keys(character)
sleep(0.1)

我无法让driver.get((工作。我知道chrome路径是正确的,因为它确实打开了一个新的chrome窗口,而没有打开网页。你能在不更改选项变量的情况下修复我的代码吗。我现在只是通过进行回溯调用来纠正错误。

错误为:

File "C:UsersviditDocumentsCODEtyperacer.py", line 14, in <module>
driver = webdriver.Chrome(chrome_path, options=option)
File "C:UsersviditAppDataLocalProgramsPythonPython38-32libsite-packagesseleniumwebdriverchromewebdriver.py", line 73, in __init__
self.service.start()
File "C:UsersviditAppDataLocalProgramsPythonPython38-32libsite-packagesseleniumwebdrivercommonservice.py", line 98, in start
self.assert_process_still_running()
File "C:UsersviditAppDataLocalProgramsPythonPython38-32libsite-packagesseleniumwebdrivercommonservice.py", line 109, in assert_process_still_running
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service C:Program FilesGoogleChromeApplicationchrome.exe unexpectedly exited. Status code was: 0

我的chrome实例没有关闭,也没有做任何事情来澄清这一点。Chrome仍在运行,没有崩溃或关闭。

构造函数是:

driver = webdriver.Chrome(executable_path=r'C:pathtochromedriver.exe', options=option)

其中第一个参数Keyexecutable_path,它指向ChromeDriver二进制文件的绝对路径,而不是chrome.exe的绝对路径。

因此,您需要通过chromedriver_path从Chrome的chromedriver-WebDriver下载来更改chrome_path的值,您的有效代码块将是:

option = webdriver.ChromeOptions()
option.add_argument("--start-maximized")
chromedriver_path = r'C:pathtochromedriver.exe'
driver = webdriver.Chrome(executable_path=chromedriver_path, options=option)
driver.get('https://play.typeracer.com/')

相关内容

  • 没有找到相关文章