如何使用Selenium Webdriver在click()之前正确等待页面加载?

时间:2016-05-17 19:13:44

标签: python selenium

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线?

3 个答案:

答案 0 :(得分:0)

您可能需要更改

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")))

答案 1 :(得分:0)

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;
}

答案 2 :(得分:0)

错误的代码但有效:

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

time.sleep(1)           我== 0