【问题标题】:Multiple conditions using python selenium webdriver wait-until使用python selenium webdriver wait-until的多个条件
【发布时间】:2021-11-24 07:12:16
【问题描述】:

我几乎可以肯定,对此几乎没有简单的解决方案,但是.. 我正在单击网页的一个元素,之后会出现超过 2 个不同页面中的一个。 比我需要单击每种结果页面的特定元素之一。
如果我有 2 个变体,我可以使用 TRY 和 EXCEPT + WebDriverWait.until 结构。

lesson = driver.find_element_by_xpath('//input[@class = "playButton"]')
lesson.click()
try:
    WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//div[@class = "cursor-hover"]')))
    start_b = driver.find_element_by_xpath('//div[@class = "cursor-hover"]')
    start_b.click()
except:
    WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, '//div[@class = "cursor-hover2"]')))
    start_g = driver.find_element_by_xpath('//div[@class = "cursor-hover2"]')
    start_g.click()

这个结构很完美。如果没有第一个元素 - 单击第二个元素。那么我可以使用什么来成功识别 5 或更多的唯一元素?

【问题讨论】:

  • 也许把try-exception放在一个循环中,成功就中断?

标签: python selenium webdriverwait multiple-conditions


【解决方案1】:

可能有一个可能的 xpath 列表并遍历它们将是最简单的解决方案。

# possible xpaths depending on what page will be opened afterwards
possible_xpaths = ['//div[@class = "cursor-hover1"]', '//*[@class="something else"]', ...]

for xpath in possible_xpaths:
    try:
        WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, xpath)))
        element = driver.find_element_by_xpath(xpath)
        element.click()
        break # found the correct element
    except:
        pass # continue to try the next xpath

【讨论】:

    【解决方案2】:

    谢谢大家! 最好的解决方案是使用循环和 xpath 列表。看起来准确且运行良好

    start_path = [
    '//div[@class = "item image vectorshape item_5VQC0609M6a textlib"]',
    '//div[@aria-label = "GetStarted01.png"]',
    '//div[@data-model-id = "6qik7pRD2IF_ResumePromptSlide_btn0"]',
    '//div[@id = "6XuNknTrhpP.6ZEBMbiB1Jo.655Ij3xXJBL.5cP2lAl8Izg.67Nc1dZxOLf"]/canvas',
    '//div[@class = "slide-object slide-object-button shown cursor-hover      "]',
    '//div[@class = "item image vectorshape item_6REUhB7StMI textlib"]'
    
    def buttonfinder(path_list):
    time.sleep(5)
    for path in path_list:
        try:
            print(path)
            WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, path))).click()
            break
        except:
            pass
    

    【讨论】:

      【解决方案3】:

      如果我理解正确,您需要像这样调整您的 xpath 可以解决您的问题,请确保 xpath 对于页面是唯一的。

      lesson = driver.find_element_by_xpath('//input[@class = "playButton"]')
      lesson.click()
      
      WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//div[@class = "cursor-hover"]|//div[@class = "cursor-hover2"]')))
      
      start_b = driver.find_element_by_xpath('//div[@class = "cursor-hover"]|//div[@class = "cursor-hover2"]')
      start_b.click()
      

      或者为了更简单,你也可以点击等待。

      WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//div[@class = "cursor-hover"]|//div[@class = "cursor-hover2"]'))).click()
      

      如果使用带有管道分隔符的 xpath 不适合并且您想坚持例外,那么我建议您先识别页面然后添加等待,否则添加 5 页的 webdriverwait 将花费太长时间才能到达第五页所以改为

      试试链接

      if driver.find_element_by_xpath('//some title or specific element of page'):
          WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, 'xpath for that page'))).click()
      

      【讨论】:

      • 难以理解的描述,对。好的,我需要检查打开的页面上是否存在几个(3 个或更多)已知元素之一并单击它
      • 好吧,您可以使用多个带有管道分隔符的 xpath,因此您不需要在有多个页面的情况下使用多个 webdriverwaits,并且添加条件会减少等待每个页面的时间,即它的夏天
      猜你喜欢
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多