Python和Selenium,无法单击提交按钮

在这种情况下,我试图使用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>```

bush200 回答:Python和Selenium,无法单击提交按钮

我会尝试仅使用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,"Username"))).send_keys("test_username")


# password - send keys
WebDriverWait(driver,"Password"))).send_keys("test_PASSWORD")

# login - click button
WebDriverWait(driver,'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()

我的代码段使用的是我在此答案顶部提供的确切的代码行,因此我不确定为什么它对您不起作用。如果此答案中的完整示例对您有用,但是单行代码在您自己的代码上下文中不起作用,则可能是其他因素干扰了此问题。

如果此答案中的完整示例对您不起作用,则问题可能出在硒之外。

,

对于元素上的click(),您必须为element_to_be_clickable()引入 WebDriverWait ,并且可以使用以下任一Locator Strategies

  • 使用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,"//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
    
本文链接:https://www.f2er.com/3072001.html

大家都在问