无法使用 Selenium 单击模式对话框中的文本字段 Python
Unable to click on text field in modal dialog using Selenium Python
我试图在用户名和密码字段上单击并选择值,但它 'modal-dialog',这让我无法完成操作。
打开 URL 并成功 运行 后立即出现模式。
我正在使用以下代码:
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get('https://app.staging.showcare.io/product-showcase')
wait = WebDriverWait(driver, 20)
wait.until(EC.title_contains("Signin"))
print('sign in')
print('wait')
userName_field = driver.find_element_by_name('username')
time.sleep(6)
userName_field.click()
userName_field.send_keys('')
driver.quit()
我知道我正在使用 time.sleep,这不是最佳做法,但我正在努力花时间。
我也用过等到点击,它不起作用。
现在,它抛出的异常是这样的:
Traceback (most recent call last):
File "/Users/tp/Documents/Pract/ShowCase_Automation/exhibitorAccess.py", line 19, in <module>
userName_field.click()
File "/Users/tp/Documents/Pract/ShowCase_Automation/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/Users/tp/Documents/Pract/ShowCase_Automation/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/Users/tp/Documents/Pract/ShowCase_Automation/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/tp/Documents/Pract/ShowCase_Automation/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=93.0.4577.82)
有人可以帮我解决这个问题吗?
该页面上有 2 个元素匹配 find_element_by_name('username')
。
要访问正确的元素,您应该更改定位器。
改用这个:
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get('https://app.staging.showcare.io/product-showcase')
wait = WebDriverWait(driver, 20)
wait.until(EC.title_contains("Signin"))
print('sign in')
print('wait')
userName_field = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'modal-content') and(contains(@class,'visible-lg'))]//input[@name='username']")))
userName_field.click()
userName_field.send_keys('')
time.sleep(6)
我试图在用户名和密码字段上单击并选择值,但它 'modal-dialog',这让我无法完成操作。 打开 URL 并成功 运行 后立即出现模式。
我正在使用以下代码:
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get('https://app.staging.showcare.io/product-showcase')
wait = WebDriverWait(driver, 20)
wait.until(EC.title_contains("Signin"))
print('sign in')
print('wait')
userName_field = driver.find_element_by_name('username')
time.sleep(6)
userName_field.click()
userName_field.send_keys('')
driver.quit()
我知道我正在使用 time.sleep,这不是最佳做法,但我正在努力花时间。 我也用过等到点击,它不起作用。 现在,它抛出的异常是这样的:
Traceback (most recent call last):
File "/Users/tp/Documents/Pract/ShowCase_Automation/exhibitorAccess.py", line 19, in <module>
userName_field.click()
File "/Users/tp/Documents/Pract/ShowCase_Automation/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/Users/tp/Documents/Pract/ShowCase_Automation/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/Users/tp/Documents/Pract/ShowCase_Automation/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/tp/Documents/Pract/ShowCase_Automation/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=93.0.4577.82)
有人可以帮我解决这个问题吗?
该页面上有 2 个元素匹配 find_element_by_name('username')
。
要访问正确的元素,您应该更改定位器。
改用这个:
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get('https://app.staging.showcare.io/product-showcase')
wait = WebDriverWait(driver, 20)
wait.until(EC.title_contains("Signin"))
print('sign in')
print('wait')
userName_field = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'modal-content') and(contains(@class,'visible-lg'))]//input[@name='username']")))
userName_field.click()
userName_field.send_keys('')
time.sleep(6)