我尝试使用以下代码将字符串放入以下网站的 txt 字段中:
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
if __name__ == '__main__':
options = Options()
options.add_argument("start-maximized")
options.add_argument('--log-level=3')
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
srv=Service()
driver = webdriver.Chrome (service=srv, options=options)
waitWD = WebDriverWait (driver, 10)
firstRun = True
driver.get ("https://translate.google.com/")
time.sleep(3)
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//button[@aria-label="Alle akzeptieren"]'))).click()
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//c-wiz[@jsdata="deferred-c2"]'))).click()
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//c-wiz[@jsdata="deferred-c2"]'))).send_keys("This is some test!")
但我只收到这个错误:
(openAIALL) C:\DEV\Fiverr\ORDER\robalf\SOLtranslateTXT>python test1.py
Traceback (most recent call last):
File "C:\DEV\Fiverr\ORDER\robalf\SOLtranslateTXT\test1.py", line 28, in <module>
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//c-wiz[@jsdata="deferred-c2"]'))).send_keys("This is some test!")
File "C:\DEV\.venv\openAIALL\lib\site-packages\selenium\webdriver\remote\webelement.py", line 231, in send_keys
self._execute(
File "C:\DEV\.venv\openAIALL\lib\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute
return self._parent.execute(command, params)
File "C:\DEV\.venv\openAIALL\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
self.error_handler.check_response(response)
File "C:\DEV\.venv\openAIALL\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=120.0.6099.200)
为什么无法在此字段中输入文本?
我包含了 try- except 块来处理元素未找到或不可单击时潜在的异常。这确保了即使元素不立即存在或可单击,脚本也不会崩溃,而是会处理这种情况。
尝试这个代码,它应该可以工作:
# Wait for the cookie consent popup and then click on the accept button
try:
accept_button = waitWD.until(
EC.element_to_be_clickable((By.XPATH, '//button[@aria-label="Alle akzeptieren"]')))
accept_button.click()
except:
print("Accept button not found or clickable.")
# Wait for the input field and then send keys
try:
input_field = waitWD.until(EC.element_to_be_clickable((By.XPATH, '//textarea[@aria-label="Source text"]')))
input_field.send_keys("This is some test!")
except:
print("Input field not found or clickable.")
我认为您的目标节点不正确 -
//c-wiz[@jsdata="deferred-c2"]
尝试定位
input
节点。使用此 XPath - //textarea[@aria-label='Source text']
代码应该是:
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//textarea[@aria-label="Source text"]'))).send_keys("This is some test!")