提问人:Huzaifa 提问时间:10/31/2023 更新时间:11/4/2023 访问量:33
如何用硒填写<表格>之外的表格?
How to fill a form outside a <form> with selenium?
问:
我正在尝试填写表格。这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import Keys
import time
driver = webdriver.Firefox()
driver.get('https://jazz.com.pk/choose-your-number')
prefixSelect = Select(driver.find_element(By.ID, 'ndc'))
criterionSelect = Select(driver.find_element(By.ID, 'searchcriteria'))
patternInput = driver.find_element(By.ID, 'pattern')
searchButton = driver.find_element(By.ID, 'submit')
prefixSelect.select_by_visible_text('0307')
criterionSelect.select_by_visible_text('First 4 Digits')
patternInput.send_keys('7384')
这是我收到的错误:
line 19, in <module>
prefixSelect.select_by_visible_text('0307')
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view
下面我发布了一系列代码块,其中包含我尝试填写表单的尝试(请注意页面上没有元素),以及我遇到的错误:<form>
尝试
prefixSelect = Select(driver.find_element(By.ID, 'ndc'))
criterionSelect = Select(driver.find_element(By.ID, 'searchcriteria'))
patternInput = driver.find_element(By.ID, 'pattern')
searchButton = driver.find_element(By.ID, 'submit')
WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, 'option[value="0303"]')))
prefixSelect.select_by_visible_text('0307')
criterionSelect.select_by_visible_text('First 4 Digits')
patternInput.send_keys('7384')
错误
line 19, in <module>
WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, 'option[value="0303"]')))
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
尝试(对于后面的所有尝试,仅包含中间的行)
prefixOption = driver.find_element(By.CSS_SELECTOR, 'option[value="0307"]')
ActionChains(driver).move_to_element(prefixOption).perform()
错误
line 20, in <module>
ActionChains(driver).move_to_element(prefixOption).perform()
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: Origin element <option> is not displayed
尝试
prefixOption = driver.find_element(By.CSS_SELECTOR, 'option[value="0307"]')
driver.execute_script('arguments[0].scrollIntoView();', prefixOption)
错误
line 22, in <module>
prefixSelect.select_by_visible_text('0307')
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view
尝试
prefixOption = driver.find_element(By.CSS_SELECTOR, 'option[value="0307"]')
driver.execute_script("arguments[0].dispatchEvent(new Event('mouseover'));", prefixOption)
错误
line 22, in <module>
prefixSelect.select_by_visible_text('0307')
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view
然后我尝试单击加载页面时已经在视图中的页面。<select>
尝试
driver.find_element(By.ID, 'ndc').click()
错误
line 19, in <module>
driver.find_element(By.ID, 'ndc').click()
selenium.common.exceptions.ElementNotInteractableException: Message: Element <select id="ndc" class="ndc form-control mdbselectcustome abc w-100 select2-hidden-accessible" name="ndc"> could not be scrolled into view
尝试
for i in range(3):
driver.find_element(By.TAG_NAME, 'body').send_keys(Keys.ARROW_DOWN)
driver.find_element(By.ID, 'ndc').click()
错误
line 22, in <module>
driver.find_element(By.ID, 'ndc').click()
selenium.common.exceptions.ElementNotInteractableException: Message: Element <select id="ndc" class="ndc form-control mdbselectcustome abc w-100 select2-hidden-accessible" name="ndc"> could not be scrolled into view
尝试
ActionChains(driver).move_to_element(driver.find_element(By.ID, 'ndc')).perform()
driver.find_element(By.ID, 'ndc').click()
错误
line 20, in <module>
driver.find_element(By.ID, 'ndc').click()
selenium.common.exceptions.ElementNotInteractableException: Message: Element <select id="ndc" class="ndc form-control mdbselectcustome abc w-100 select2-hidden-accessible" name="ndc"> could not be scrolled into view
尝试
driver.execute_script('arguments[0].scrollIntoView();', driver.find_element(By.ID, 'ndc'))
driver.find_element(By.ID, 'ndc').click()
错误
line 20, in <module>
driver.find_element(By.ID, 'ndc').click()
selenium.common.exceptions.ElementNotInteractableException: Message: Element <select id="ndc" class="ndc form-control mdbselectcustome abc w-100 select2-hidden-accessible" name="ndc"> could not be scrolled into view
尝试
driver.execute_script("arguments[0].dispatchEvent(new Event('mouseover'));", driver.find_element(By.ID, 'ndc'))
driver.find_element(By.ID, 'ndc').click()
错误
line 20, in <module>
driver.find_element(By.ID, 'ndc').click()
selenium.common.exceptions.ElementNotInteractableException: Message: Element <select id="ndc" class="ndc form-control mdbselectcustome abc w-100 select2-hidden-accessible" name="ndc"> could not be scrolled into view
经过所有这些尝试,也许我可以在互联网上搜索更多(可能只是 StackOverflow)并了解如何填写此表格,但我只是决定在这里问。
请告诉我如何填写此表格。
答:
0赞
Yaroslavm
11/4/2023
#1
元素具有自定义呈现的逻辑。select 实际上被另一个元素覆盖,并且不可见。
要与下拉列表交互,您需要与可见且可单击的元素交互。
表示下拉列表的可见元素具有选择器。[aria-labelledby=select2-ndc-container]
您只需单击它并等待选项选择器呈现下拉选项。.select2-results [role=option]
之后,您可以使用文本完全匹配过滤器过滤元素,单击它并等待下拉选项的过时性。
wait = WebDriverWait(driver, 10)
dropdown = driver.find_element(By.CSS_SELECTOR, '[aria-labelledby=select2-ndc-container]')
dropdown.click()
options = wait.until(expected_conditions.visibility_of_all_elements_located((By.CSS_SELECTOR, '.select2-results [role=option]')))
[element for element in options if element.text == '0307'][0].click()
wait.until(expected_conditions.staleness_of(options[0]))
评论