英文:
logging into modal form with python selenium
问题
I'm trying to log into https://www.racingandsports.com.au/. the login link opens a modal form but I'm having trouble with sending user/pass to it. I'm using the below code.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.racingandsports.com.au/")
sign_in_button = driver.find_element(by=By.ID, value="btn_login")
sign_in_button.click()
username_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "lb-username")))
password_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "lb-password")))
username_field.send_keys("your_username")
password_field.send_keys("your_password")
#login_button = driver.find_element(By.ID, value="btnLogin").click()
#WebDriverWait(driver, 10).until(EC.url_contains("https://www.racingandsports.com.au/"))
time.sleep(10)
#driver.quit()
I can open the modal ok from the link but when trying to send user/pass to it I'm getting:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Can anyone point me in the right direction please, thanks!
英文:
Im trying to log into https://www.racingandsports.com.au/. the login link opens a modal form but Im having trouble with sending user/pass to it. I'm using the below code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.racingandsports.com.au/")
sign_in_button = driver.find_element(by=By.ID, value="btn_login")
sign_in_button.click()
username_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "lb-username")))
password_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "lb-password")))
username_field.send_keys("your_username")
password_field.send_keys("your_password")
#login_button = driver.find_element(By.ID, value = "btnLogin").click()
#WebDriverWait(driver, 10).until(EC.url_contains("https://www.racingandsports.com.au/"))
time.sleep(10)
#driver.quit()
I can open the modal ok from the link but when trying to send user/pass to it I'm getting
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
can anyone point me in the right direction please, thanks!
答案1
得分: 2
SeleniumBase 可以处理该网站:
pip install seleniumbase
,然后使用 python
或 pytest
运行以下代码:
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
class ExampleTest(BaseCase):
def test_example(self):
self.open("https://www.racingandsports.com.au/")
self.click("a#btn_login")
self.type('input[placeholder*="Username"]', "your_username")
self.type('input[placeholder*="Password"]', "your_password")
self.sleep(10)
英文:
SeleniumBase can handle that site:
pip install seleniumbase
, then run the following with python
or pytest
:
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
class ExampleTest(BaseCase):
def test_example(self):
self.open("https://www.racingandsports.com.au/")
self.click("a#btn_login")
self.type('input[placeholder*="Username"]', "your_username")
self.type('input[placeholder*="Password"]', "your_password")
self.sleep(10)
答案2
得分: 0
你的脚本基本正确。我做了一些更改,现在它可以正常工作。
-
网站加载较慢,因此你会希望在第一个
.find_element()
上使用WebDriverWait
,就像你在其他地方一样。 -
你等待的是元素的存在。存在仅意味着该元素在DOM中,但不一定是可交互的。如果你要点击某物,使用
EC.element_to_be_clickable()
,如果你要与元素交互,比如.send_keys()
或获取.text
,则使用EC.visibility_of_element_located()
。
下面的代码可以正常工作:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.racingandsports.com.au/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "btn_login"))).click()
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lb-username"))).send_keys("your_username")
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lb-password"))).send_keys("your_password")
wait.until(EC.element_to_be_clickable((By.ID, "btnLogin"))).click()
# 继续脚本,确保对于第一个元素(至少)你添加了另一个等待
driver.quit()
英文:
Your script was pretty close. I made a couple changes and it's working fine.
-
The site is slow to load. Because of that, you'll want to use a
WebDriverWait
on the first.find_element()
as you did with the others. -
You were waiting for presence. Presence just means that the element is in the DOM but not necessarily interactable. Use
EC.element_to_be_clickable()
if you are going to click something andEC.visibility_of_element_located()
if you are going to interact with an element like.send_keys()
or get.text
.
The code below is working.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.racingandsports.com.au/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "btn_login"))).click()
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lb-username"))).send_keys("your_username")
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lb-password"))).send_keys("your_password")
wait.until(EC.element_to_be_clickable((By.ID, "btnLogin"))).click()
# continue script, make sure for the first element (at least) you add another wait
driver.quit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论