錯誤。元素當前不可互動,不能被操作。
因此,我讓 Selenium 請求訪問一個頁面,然后填寫一些輸入框,并點擊一個按鈕來提交表單。
我遇到的問題是,Selenium 沒有等待元素可見,所以我必須手動放置 time.sleep(2) 來解決這個問題,但我更希望使用等待,直到它們顯示出來,以防加載時間更長或更短,或者根本就沒有加載的情況。
error
Traceback (most recent call last):
檔案 "C:UsersexampleAppDataRoamingJetBrainsPyCharmCE2021.2scratchesexample.py", line 125, in <module>
completeForm(
檔案 "C:UsersexampleAppDataRoamingJetBrainsPyCharmCE2021.2scratchesexample.py", line 89, in completeForm
typeText(Field, text)
檔案 "C:UsersexampleAppDataRoamingJetBrainsPyCharmCE2021.2scratchesexample.py", 行 50, in typeText
element.clear()
檔案 "C:UsersexampleDesktopselenium-programvenvlibsite-packagesseleniumwebdriver
emotewebelement.py", 行 92, in clear
self._execute(Command.CLEAR_ELEMENT)
檔案 "C:UsersexampleDesktopselenium-programvenvlibsite-packagesseleniumwebdriver
emotewebelement.py",行693,in_execute
return self._parent.execute(command, params)
檔案 "C:UsersexampleDesktopselenium-programvenvlibsite-packagesseleniumwebdriver
emotewebdriver.py", 行 400, in execute
self.error_handler.check_response( response)
檔案 "C:UsersexampleDesktopselenium-programvenvlibsite-packagesseleniumwebdriver
emoteerrorhandler.py",第236,in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidElementStateException。訊息:無效的元素狀態。元素是 不是當前可互動的和可能不是被操縱的
(會話資訊: chrome=93.0.4577.63)
代碼
from seleniumwire.undetected_chromedriver.2 import Chrome, ChromeOptions
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import random
import時間
#setup selenium chrome browser with options[/span]。
def setupBrowser()。
global driver
選項= {}。
chrome_options = ChromeOptions()
驅動 = Chrome(seleniumwire_options=options, options=chrome_options)
#Type like a human[/span
def typeText(element, text)。
success = False。
if doesElementExist(Element):
while success == False:
actions = ActionChains(driver)
actions.move_to_element(element)
actions.click()
元素.清除()
for character in text。
actions.send_keys(character)
行動.執行()
time.sleep(random.uniform(0.13, 0.4)
if element.get_attribute("data-initial-value") == text:
success = True
#check if element exists, times out after some time and returns error.
def doesElementExist(element)。
timeout = 10
try:
element_present = EC.visibility_of_element_located(element)
WebDriverWait(driver, timeout).until(Element_present)
except TimeoutException:
print("錯誤。在等待元素加載的{}秒后超時!".format(timeout))
return False。
finally:
return True: return True
驅動程式.get(url)
driver.implicitly_wait(60)
Field = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div[2]/div/div[1]/input'/span>)
text = "exampletexttowrite123"/span>
typeText(Field, text)
uj5u.com熱心網友回復:
你的typeText需要改正。它沒有.perform(),所以發生的情況是所有的專案將被存盤在Actioncahins佇列中,當你寫下.perform()時,它們將被一個一個地釋放。
代碼 :
def typeText(element, text)。
success = False。
if doesElementExist(Element):
while success == False:
actions = ActionChains(driver)
actions.move_to_element(element).perform()
element.click()
元素.清除()
for character in text。
actions.send_keys(character).perform()
time.sleep(random.uniform(0.13, 0.4)
if element.get_attribute("data-initial-value") == text:
success = True
PS :
這個方法直接接受一個網路元素,確保網路元素應該被正確呈現。
Update :
driver = webdriver.Chrome(driver_path)
driver.maximum_window()
#driver.implicitly_wait(50)/span>
driver.get("https://docs.google.com/forms/d/e/1FAIpQLSfMJRFOPF0qeJG3DHrbzbDR7nETPF0qE2D-r_F3kjjqdP9B1w/viewform")
wait = WebDriverWait(driver, 20)
ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Your answer']/preceding-sibling::input"))))。執行()
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Your answer']/preceding-sibling::input")).send_keys(' cruise')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318369.html
標籤:
