登录模态表单使用Python Selenium。

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

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.

  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.common.keys import Keys
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.common.by import By
  7. driver = webdriver.Chrome()
  8. driver.get("https://www.racingandsports.com.au/")
  9. sign_in_button = driver.find_element(by=By.ID, value="btn_login")
  10. sign_in_button.click()
  11. username_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "lb-username")))
  12. password_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "lb-password")))
  13. username_field.send_keys("your_username")
  14. password_field.send_keys("your_password")
  15. #login_button = driver.find_element(By.ID, value="btnLogin").click()
  16. #WebDriverWait(driver, 10).until(EC.url_contains("https://www.racingandsports.com.au/"))
  17. time.sleep(10)
  18. #driver.quit()

I can open the modal ok from the link but when trying to send user/pass to it I'm getting:

  1. raise exception_class(message, screen, stacktrace)
  2. 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

  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.common.by import By
  6. driver = webdriver.Chrome()
  7. driver.get("https://www.racingandsports.com.au/")
  8. sign_in_button = driver.find_element(by=By.ID, value="btn_login")
  9. sign_in_button.click()
  10. username_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "lb-username")))
  11. password_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "lb-password")))
  12. username_field.send_keys("your_username")
  13. password_field.send_keys("your_password")
  14. #login_button = driver.find_element(By.ID, value = "btnLogin").click()
  15. #WebDriverWait(driver, 10).until(EC.url_contains("https://www.racingandsports.com.au/"))
  16. time.sleep(10)
  17. #driver.quit()

I can open the modal ok from the link but when trying to send user/pass to it I'm getting

  1. 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 运行以下代码:

  1. from seleniumbase import BaseCase
  2. BaseCase.main(__name__, __file__)
  3. class ExampleTest(BaseCase):
  4. def test_example(self):
  5. self.open("https://www.racingandsports.com.au/")
  6. self.click("a#btn_login")
  7. self.type('input[placeholder*="Username"]', "your_username")
  8. self.type('input[placeholder*="Password"]', "your_password")
  9. self.sleep(10)
英文:

SeleniumBase can handle that site:

pip install seleniumbase, then run the following with python or pytest:

  1. from seleniumbase import BaseCase
  2. BaseCase.main(__name__, __file__)
  3. class ExampleTest(BaseCase):
  4. def test_example(self):
  5. self.open("https://www.racingandsports.com.au/")
  6. self.click("a#btn_login")
  7. self.type('input[placeholder*="Username"]', "your_username")
  8. self.type('input[placeholder*="Password"]', "your_password")
  9. self.sleep(10)

答案2

得分: 0

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

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

  2. 你等待的是元素的存在。存在仅意味着该元素在DOM中,但不一定是可交互的。如果你要点击某物,使用EC.element_to_be_clickable(),如果你要与元素交互,比如.send_keys()或获取.text,则使用EC.visibility_of_element_located()

下面的代码可以正常工作:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. driver = webdriver.Chrome()
  6. driver.maximize_window()
  7. driver.get("https://www.racingandsports.com.au/")
  8. wait = WebDriverWait(driver, 10)
  9. wait.until(EC.element_to_be_clickable((By.ID, "btn_login"))).click()
  10. wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lb-username"))).send_keys("your_username")
  11. wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lb-password"))).send_keys("your_password")
  12. wait.until(EC.element_to_be_clickable((By.ID, "btnLogin"))).click()
  13. # 继续脚本,确保对于第一个元素(至少)你添加了另一个等待
  14. 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.

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. driver = webdriver.Chrome()
  6. driver.maximize_window()
  7. driver.get("https://www.racingandsports.com.au/")
  8. wait = WebDriverWait(driver, 10)
  9. wait.until(EC.element_to_be_clickable((By.ID, "btn_login"))).click()
  10. wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lb-username"))).send_keys("your_username")
  11. wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "lb-password"))).send_keys("your_password")
  12. wait.until(EC.element_to_be_clickable((By.ID, "btnLogin"))).click()
  13. # continue script, make sure for the first element (at least) you add another wait
  14. 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:

确定