在Python中使用Selenium登录

在Python中使用Selenium登录,python,selenium,xpath,iframe,css-selectors,Python,Selenium,Xpath,Iframe,Css Selectors,我正在尝试使用Python中的Selenium来尝试登录这个网站 https://commerce.spscommerce.com/auth/login/ 我尝试过的所有xpath都无法定位该元素 driver = webdriver.Chrome() driver.get("https://commerce.spscommerce.com/auth/login/") driver.find_element_by_xpath('//input[@id="username"]').send_ke

我正在尝试使用Python中的Selenium来尝试登录这个网站

https://commerce.spscommerce.com/auth/login/
我尝试过的所有xpath都无法定位该元素

driver = webdriver.Chrome()
driver.get("https://commerce.spscommerce.com/auth/login/")
driver.find_element_by_xpath('//input[@id="username"]').send_keys("test")
我尝试使用名称、类型和类,但未能找到元素。 这是我从inspect元素得到的HTML

<input id="username" name="username" type="email" required="" ng-model="ctrl.email" ng-
blur="ctrl.checkEmail($event, ctrl.email)" ng-focus="ctrl.resetEmailBoolean()" autofocus="" 
test="login-username" class="ng-pristine ng-valid ng-not-empty ng-valid-email ng-valid-required ng-touched">

关于使用什么路径来定位元素并发送用户名/密码,您有什么想法吗?非常感谢您的帮助。

电子邮件地址字段位于
中,因此您必须:

  • 诱导WebDriverWait以使所需帧可用并切换到该帧
  • 诱导WebDriverWait使所需元素可单击
  • 您可以使用以下任一选项:

    • 使用
      CSS\u选择器

      driver.get('https://commerce.spscommerce.com/auth/login/')
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://commerce.spscommerce.com/auth-app']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#username"))).send_keys("test")
      
    • 使用
      XPATH

      driver.get("https://www.nasdaq.com/symbol/msft/interactive-chart?timeframe=5d")
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src, 'edgar-chartiq')]")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='btn hideSmallIR stx-collapsible' and @id='dataTableBtn']/span[text()='Data Table']"))).click()
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
  • 浏览器快照:


参考文献 您可以在以下内容中找到相关讨论:

电子邮件地址字段位于
中,因此您必须:

  • 诱导WebDriverWait以使所需帧可用并切换到该帧
  • 诱导WebDriverWait使所需元素可单击
  • 您可以使用以下任一选项:

    • 使用
      CSS\u选择器

      driver.get('https://commerce.spscommerce.com/auth/login/')
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://commerce.spscommerce.com/auth-app']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#username"))).send_keys("test")
      
    • 使用
      XPATH

      driver.get("https://www.nasdaq.com/symbol/msft/interactive-chart?timeframe=5d")
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src, 'edgar-chartiq')]")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='btn hideSmallIR stx-collapsible' and @id='dataTableBtn']/span[text()='Data Table']"))).click()
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
  • 浏览器快照:


参考文献 您可以在以下内容中找到相关讨论: