【问题标题】:How to properly wait until page loads before click() using Selenium Webdriver? [duplicate]如何使用 Selenium Webdriver 在 click() 之前正确等待页面加载? [复制]
【发布时间】:2016-09-14 01:07:21
【问题描述】:
driver.find_element_by_id("ch4_loginGuestBtn").click()

此命令间歇性失败,并显示:

"Element is not clickable"

我猜我需要执行以下操作。

    guestlogin_button = wait.until(EC.visibility_of_element_located((By.id, ".ch4_loginGuestBtn")))
    actions = ActionChains(driver)
    actions.move_to_element(guestlogin_button).click().perform()

然而,这失败了:

AttributeError: type object 'By' has no attribute 'id'

我做错了什么,如何修复 wait.until 行?

【问题讨论】:

  • 我将 id 更改为 Id,仍然是同样的问题。您链接的帖子似乎没有帮助。
  • By 中的其他项目使用大写(By.CSS_SELECTOR、By.XPATH),所以我会尝试By.ID。
  • 更改为 ID 解决了问题。

标签: python selenium


【解决方案1】:

你可能需要改变

guestlogin_button = wait.until(EC.visibility_of_element_located((By.id, ".ch4_loginGuestBtn")))

到

guestlogin_button = wait.until(EC.visibility_of_element_located((By.ID, ".ch4_loginGuestBtn")))

【讨论】:

    【解决方案2】:

    错误的代码但有效:

    i=0
    while i<1:
       try:
          driver.find_element_by_id("ch4_loginGuestBtn").click()
          i==1
       except:
    

    时间.sleep(1) 我==0

    【讨论】:

      【解决方案3】:
      int count=0;
      while (count < 4) {
          try {
              Webdriverwait wait = new Webdriverwait(driver,10);
              WebElement p = wait.until(ExpectedConditions.elementToBeClickable(By.xapth("")));
              driver.findelement(by.xapth("")).click
           } 
           catch (Exception e) 
           {
               System.out.println("Trying to recover from a exception :-");
               count = count + 1;
               continue;
           } 
      
           count = count + 4;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-01-31
        • 2012-07-07
        • 2017-09-18
        • 1970-01-01
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多