登录模态表单使用Python Selenium。

huangapple go评论67阅读模式
英文:

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,然后使用 pythonpytest 运行以下代码:

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

你的脚本基本正确。我做了一些更改,现在它可以正常工作。

  1. 网站加载较慢,因此你会希望在第一个.find_element()上使用WebDriverWait,就像你在其他地方一样。

  2. 你等待的是元素的存在。存在仅意味着该元素在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.

  1. 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.

  2. 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 and EC.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()

huangapple
  • 本文由 发表于 2023年4月17日 01:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定