而 运行 Selenium 在网页 python 中被破坏 UI

while running Selenium getting broken UI in webpage python

硒有问题。尝试解析一个网页,而 运行 它已损坏 UI,因此无法用文本填充字段(出现无法交互的错误)

总体情况 在 运行 selenium

之后我有什么

Message: element not interactable (Session info: chrome=99.0.4844.84) Stacktrace: Backtrace: Ordinal0 [0x00BF9943+2595139] Ordinal0 [0x00B8C9F1+2148849] Ordinal0 [0x00A843F0+1065968] Ordinal0 [0x00AAC4D3+1230035] Ordinal0 [0x00AABB8B+1227659] Ordinal0 [0x00ACB9EC+1358316] Ordinal0 [0x00AA7474+1209460] Ordinal0 [0x00ACBC04+1358852] Ordinal0 [0x00ADBAF2+1424114] Ordinal0 [0x00ACB806+1357830] Ordinal0 [0x00AA6086+1204358] Ordinal0 [0x00AA6F96+1208214] GetHandleVerifier [0x00D9B232+1658114] GetHandleVerifier [0x00E5312C+2411516] GetHandleVerifier [0x00C8F261+560433] GetHandleVerifier [0x00C8E366+556598] Ordinal0 [0x00B9286B+2173035] Ordinal0 [0x00B975F8+2192888] Ordinal0 [0x00B976E5+2193125] Ordinal0 [0x00BA11FC+2232828] BaseThreadInitThunk [0x773EFA29+25] RtlGetAppContainerNamedObjectPath [0x77AA7A7E+286] RtlGetAppContainerNamedObjectPath [0x77AA7A4E+238]

我的代码:

url = "https://opi.dfo.kz/p/ru/archive-publication/corporative-events-2020-14-07"
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get(url)
element = driver.find_element(By.XPATH, '//*[@id="CorporativeEvents20201407"]/div[1]/div[1]/div/div[2]/div[1]/span[3]')
element.send_keys('010140000143')

请帮帮我!

您不能将文本直接发送到该元素。
您应该单击该元素,这将打开另一个 input 元素,您可以在其中插入文本输入。
您还必须在此处使用预期条件显式等待。
这有效:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://opi.dfo.kz/p/ru/archive-publication/corporative-events-2020-14-07"
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="CorporativeEvents20201407"]/div[1]/div[1]/div/div[2]/div[1]/span[3]'))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@class='editor-text tip']"))).send_keys("010140000143")

'//*[@id="CorporativeEvents20201407"]/div[1]/div[1]/div/div[2]/div[1]/span[3]' XPath 定位器也可以而且应该得到改进。您可以使用 //div[@class='logic-group-condition' and .//span[@field-name='tbOpiActiveRevisions_flBin']] 代替。这个定位器更可靠。所以你的代码将是:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://opi.dfo.kz/p/ru/archive-publication/corporative-events-2020-14-07"
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='logic-group-condition' and .//span[@field-name='tbOpiActiveRevisions_flBin']]"))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@class='editor-text tip']"))).send_keys("010140000143")

这是应用我的代码后网页的屏幕截图: