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

2024-05-06 14:58:52 发布

您现在位置:Python中文网/ 问答频道 /正文

driver.find_element_by_id("ch4_loginGuestBtn").click()

此命令间歇性失败:

^{pr2}$

我想我需要做如下的事情。在

    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'

我做错了什么,我该如何修复等等,直到行吗?在


Tags: 命令actionsidbydriverbuttonelementfind
3条回答

错误的代码但有效:

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

在时间。睡觉(一) i==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;
}

你可能需要改变

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

^{pr2}$

相关问题 更多 >