Python 是否有任何方法可以限制硒找到元素的时间?

Python 是否有任何方法可以限制硒找到元素的时间?,python,selenium,selenium-chromedriver,Python,Selenium,Selenium Chromedriver,我正在尝试使用Python Selenium Chrome驱动程序自动化Google地图,我的代码可以稳定地获得我所需要的,但是没有性能时间 我使用迭代代码通过Google Maps获取商户数据,使用Selenium获取1个商户数据的时间速率低于1秒,但当代码捕获到异常时,将需要非常长的时间,比如6-7秒 以下是执行时间的一些摘要: (正常执行时间) (在开放时间发生异常时的执行时间太长) (发生异常时的执行时间,有时按预期运行) 以下是获取开放时间列表的一些代码: try: ope

我正在尝试使用Python Selenium Chrome驱动程序自动化Google地图,我的代码可以稳定地获得我所需要的,但是没有性能时间

我使用迭代代码通过Google Maps获取商户数据,使用Selenium获取1个商户数据的时间速率低于1秒,但当代码捕获到异常时,将需要非常长的时间,比如6-7秒

以下是执行时间的一些摘要:

(正常执行时间)

(在开放时间发生异常时的执行时间太长)

(发生异常时的执行时间,有时按预期运行)

以下是获取开放时间列表的一些代码:

try:
      openhour = wait(driver, 1).until(EC.visibility_of_element_located((By.XPATH, 
                           './/span[@class="section-open-hours-button maps-sprite-pane-info-arrowup"]')))

      driver.execute_script("arguments[0].scrollIntoView(true);", openhour)
      openhour.click()
      openhourstacked = wait(driver, 1).until(EC.visibility_of_element_located((By.CLASS_NAME, 
                                  'section-open-hours-container-hoverable'))).get_attribute("aria-label")
      openhourlist = openhourstacked.split(",")
      openhourlist[len(openhourlist) - 1] = openhourlist[len(openhourlist) - 1].split(".")
      openhourlist[len(openhourlist) - 1] = openhourlist[len(openhourlist) - 1][0]

      except NoSuchElementException:
         openhourlist = []
         print("No Open Hour list in this merchant!")
         openhour_trig = True
      except WebDriverException:
         openhourlist = []
         print("Failed to load Open Hour list in this merchant!")
         openhour_trig = True
很多建议说,为了正确地执行显式等待,我应该使用WebDriverWait和EC(预期条件)的组合,我直接用上面的代码进行了尝试,但WebDriverWait似乎不适用于所有传入的异常,换句话说,有时成功,有时失败

我试图用
EC.presence\u-of-all\u-element\u-located
替换
EC.visibility\u-of-all\u-element\u-located
,但没有任何区别

我希望在发生任何异常时使执行时间仍然平稳,除了上述方法之外,还有什么方法可以使Selenium在超时时停止查找元素?或者这是一个互联网连接问题?欢迎有任何想法

更新: 因此,我将开放时间代码修改为类似这样的内容

try:
              if len(driver.find_elements(By.XPATH, './/span[@class="section-open-hours-button maps-sprite-pane-info-arrowup"]'))>0:
                        openhour = wait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, 
                                       './/span[@class="section-open-hours-button maps-sprite-pane-info-arrowup"]')))
                        #openhour = driver.find_element_by_xpath('.//span[@class="section-open-hours-button maps-sprite-pane-info-arrowup"]')
                        driver.execute_script("arguments[0].scrollIntoView(true);", openhour)
                        openhour.click()
                        #openhourstacked = driver.find_element_by_class_name('section-open-hours-container-hoverable').get_attribute("aria-label")
                        openhourstacked = wait(driver, 5).until(EC.visibility_of_element_located((By.CLASS_NAME, 
                                              'section-open-hours-container-hoverable'))).get_attribute("aria-label")
                        openhourlist = openhourstacked.split(",")
                        openhourlist[len(openhourlist) - 1] = openhourlist[len(openhourlist) - 1].split(".")
                        openhourlist[len(openhourlist) - 1] = openhourlist[len(openhourlist) - 1][0]
                    else:
                        openhourlist = []
                        print("No Open Hour list in this merchant! (Not an Exception)")
                except NoSuchElementException:
                    openhourlist = []
                    print("No Open Hour list in this merchant!")
                    openhour_trig = True
                except WebDriverException:
                    openhourlist = []
                    print("Failed to load Open Hour list in this merchant!")
                    openhour_trig = True
是的,这种方法确实避免了异常,但这会像以前一样重复搜索,因此当元素未找到时,可能会再次进行长达6-7秒的长搜索。。显式等待在这些术语中也没有真正的帮助,我想是的显式等待(webdriver wait)解决了这个问题。。否则,简单的方法是创建一个timeseconds的for循环,并在循环中写入find元素,因此即使失败,它也会检查180秒,并在预期等待时间之外写入条件

  for (int second = 0; second < 60; second++) {
  try {} 
  catch (WebDriverException we) 
  {}
  if (second >= 60) {
  //write failure 
  }
for(int second=0;second<60;second++){
试试{}
catch(WebDriverException-we)
{}
如果(秒>=60){
//写入失败
}

摘要:

在您的代码中,尝试增加显式等待的时间,但不会 让您慢下来,但它会阻止这些异常。尝试5秒而不是1秒。尝试此
等待(驱动程序,5)


我们使用等待是因为加载DOM需要时间,有时我们需要等待页面/元素加载/隐藏/更改,我们必须根据更改采取行动。
显式等待的作用是在给定时间内等待预期条件的满足,并每500毫秒检查一次条件是否满足。这样,在执行下一个任务之前,您将损失最多500毫秒的时间。如果条件没有及时满足,它将抛出超时异常。显式等待将不能解决你所有的问题

您仍然需要检查是否存在可能的异常,并决定在出现异常时应采取的措施。某些站点加载速度较慢,或者您的带宽有时较低。这样您就无法加快处理速度

您必须首先分析页面并确定预期结果。如果您正在等待某个元素出现,但它可能不存在,那么您应该相应地处理异常

如果您正在检查某个元素是否存在于某个页面中,那么最好不要等待该元素,而是尝试使用
find\u elements\uu
并检查列表的大小。这样可以避免异常,而且速度更快

driver.find_elements(By.ID, "locator").size()>0

我知道这已经是一个很长的问题,并且得到了解决方案,忘记了再次更新,但是我想,我意识到将隐式等待设置为几秒钟,例如:像这样的2秒钟
驱动程序。隐式等待(2)
将有助于将WebDriver搜索限制在最后几秒钟,超过最后几秒钟,它将抛出异常

很好的解决方法@S Ahmed,因此使用size方法查找\u元素可以用作if语句?,我将尽快尝试,然后再次尝试。我尝试仅使用driver.find\u元素(By.ID,“locator”)检查大小.size()>0,但它不断重复Selenium以查找长时间的元素。不知道为什么等待会使执行时间变长,仅当加载元素需要时间或根本不加载时。您是否尝试过更改web驱动程序页面加载策略?您可以尝试这样做以加快执行时间。是的,我曾经使用imp将PageLoadStrategy设置为“无”ort需要功能,但在运行它时,它无法加载Google地图,因此我无能为力,下面是我以前设置它的方式..尝试将它设置为
eager
,因为它至少会等待
DOMContentLoad
,我不确定是否可以使用循环来捕获异常,从而加快速度,但感谢您的建议:)至少它对r在某些情况下,但有时并非每次都有效。。