簡體   English   中英

Python 和 Selenium,無法點擊提交按鈕

[英]Python and Selenium, cannot click on a submit button

在這種情況下,我嘗試使用 Selenium webdriver 單擊提交按鈕,但到目前為止無法單擊該元素。

<button type="submit" class="pcty-button pcty-size-medium pcty-button-full-width pcty-button-full-width-mobile pcty-size-responsive submit-button login-button">Login</button>

我現在必須找到按鈕的代碼如下:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit' and normalize-space()='Login']"))).click()

我認為這段代碼涵蓋了所有的基礎,並希望它點擊按鈕,但到目前為止還沒有運氣。 我在其他代碼中看到的每一個錯誤都會引發元素不存在或無法定位。

我得到的錯誤是:

引發 TimeoutException(消息,屏幕,堆棧跟蹤)selenium.common.exceptions.TimeoutException

我已經使用了一些答案來嘗試從這些線程中找到解決方案: 12 ,但截至目前,我不知道我做錯了什么。

我被要求提供此網頁的 HTML 的大部分內容以獲取上下文。

如果我需要擴展更多內容,我很樂意這樣做。 提供的答案已經過測試,並提出了相同的超時錯誤。

      <div class="pcty-col pcty-padding-top pcty-large-padding-horizontal">
        <div class="pcty-input pcty-input-checkbox">
          <label class="pcty-checkbox-label pcty-input-label" for="IsRememberingUser">
            <input type="checkbox" checked="checked" data-val="true" data-val-required="The IsRememberingUser field is required." id="IsRememberingUser" name="IsRememberingUser" value="true">Remember My Username
          </label>
        </div>
      </div>
    </div>

  <div class="pcty-row-flex">
    <div class="pcty-col pcty-padding-top pcty-large-padding-horizontal">
      <button type="submit" class="pcty-button pcty-size-medium pcty-button-full-width pcty-button-full-width-mobile pcty-size-responsive submit-button login-button">Login</button>
    </div>
  </div>```

而不是normalize-space() ,我會嘗試使用contains(text(), 'Login')

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Login')]"))).click()

由於上述解決方案似乎對您不起作用,因此我還編寫了一個完整的代碼示例,該示例成功地針對您提供的頁面鏈接運行。 我能夠使用以下命令成功發送憑據並單擊登錄按鈕:

from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


driver=webdriver.Chrome()
driver.get("https://access.paylocity.com/")

# company id - send keys
WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "CompanyId"))).send_keys("test_company_id")

# username - send keys
WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "Username"))).send_keys("test_username")


# password - send keys
WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "Password"))).send_keys("test_PASSWORD")

# login - click button
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Login')]"))).click()

# result: "The credentials provided are incorrect", meaning login button click succeeded

sleep(10) # explicit sleep here so i can visually observe the results on browser
driver.close()
driver.quit()

我的代碼片段使用了我在這個答案頂部提供的確切代碼行,所以我不確定它為什么不適合你。 如果此答案中的完整示例對您有用,但單行代碼在您自己的代碼的上下文中不起作用,那么一定是其他原因導致了此問題。

如果此答案中的完整示例不適合您,則問題可能不在 Selenium 范圍內。

要在元素上click() ,您必須為element_to_be_clickable()誘導WebDriverWait ,並且您可以使用以下任一定位器策略

  • 使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.pcty-button.pcty-size-medium.pcty-button-full-width.pcty-button-full-width-mobile.pcty-size-responsive.submit-button.login-button[type='submit']"))).click()
  • 使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='pcty-button pcty-size-medium pcty-button-full-width pcty-button-full-width-mobile pcty-size-responsive submit-button login-button' and text()='Login']"))).click()
  • 注意:您必須添加以下導入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM