【问题标题】:Identify and click button in unordered list if condition met如果条件满足,识别并单击无序列表中的按钮
【发布时间】:2021-09-19 15:15:47
【问题描述】:

我正在使用Python/Selenium 访问网页上的unordered 列表。我使用find_element_by_xpath 来正确识别我所追求的 div(text "$x.xx" 几乎总是唯一的)。

然后我想指示代码按下驻留在我已识别的 div 中的购买按钮(假设同一列表上还有 100 个其他“购买”按钮)。

但是有一个条件,在下面的两个示例中,'a classes' includes the text "disabled". 之一如果已识别具有正确文本的 div,但 'a class' includes "disabled",我希望脚本循环并再次运行 1 秒稍后。

总之,如果我在下面搜索 $50.00,代码会循环,如果我在搜索 $30.00,它会点击()该特定 div 中的“购买”按钮。

任何帮助将不胜感激。

(如果以下格式不正确,请见谅,第一次发帖!)

Python 代码:

from selenium import webdriver

PATH = "C:\\Users\\abc\\Desktop\\Automation\\chromedriver.exe"
    
driver = webdriver.Chrome(PATH)
    
findClick = driver.find_element_by_xpath("//*[text()='$30.00']")

网页:

 <ul>

    <li class="List-row">
         <div class="List-rowContent Market">
              <div class="Market-item Total"><span>$50.00</span></div>
              <div class="Market-item Buy">
                    <a class="Button-small Market-Buy disabled" data-dialog="#buy_start">Purchase</a></div></div></li>
    
    <li class="List-row">
         <div class="List-rowContent Market">
              <div class="Market-item Total"><span>$30.00</span></div>
              <div class="Market-item Buy">
                   <a class="Button-small Market-Buy" data-dialog="#buy_start">Purchase</a></div></div></li>

【问题讨论】:

    标签: python google-chrome selenium-webdriver selenium-chromedriver


    【解决方案1】:

    根据您分享的HTML,以下xpath

    //span[text()='$30.00']/../following-sibling::div/a[text()='Purchase']
    

    根据价格文本表示Purchase按钮,您可以将其更改为50$并签入HTMLDOM

    现在来回答这个问题

    但是,在下面的两个示例中,有一个条件,其中一个 'a classes' 包括文本“disabled”。如果 div 正确 文本已被识别,但“a 类”包括“已禁用”,我 希望脚本循环并在 1 秒后再次运行。

    如果我理解正确,下面的代码应该可以工作。

    try:
        if len(driver.find_elements(By.XPATH, "//span[text()='$30.00']/../following-sibling::div/a[text()='Purchase']")) > 0:
            print("$30.00 is visible so bot should click it directly")
            WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='$30.00']/../following-sibling::div/a[text()='Purchase']"))).click()
        else:
            print("$30.00 was not visible so loop it here")
            for i in range(100):
                print("Inside else for loop for the nth time", i)
                time.sleep(20) # for each iteration wait for 20 secs
                #code logic here 
    except:
        print("Something went wrong")
        pass
    

    进口:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    【讨论】:

    • 完美,太棒了!冒着听起来难以置信的愚蠢的风险,下面的部分:“for i in range(some range here): #do some stuff”.....这个范围是我告诉它运行的列表项的数量,即(100) 会经过 100
    • ..... 是否也可以将父 div 类声明为范围?.....那么#do some stuff,那是我写循环的地方吗?这实际上是我写过的第一个脚本,所以您能否提供一个基本示例,以便我可以使用它?再次感谢您的时间和耐心
  • 我不明白这部分Is it also possible to declare a parent div class as a range range 期望整数值,你可以在它上面使用 find_elements (复数)和 len 使其返回 int 值,就像我在上面所做的那样。
  • 好的,非常感谢,很抱歉造成混乱。所以本质上,如果我将范围整数设置为 100,这是否意味着它循环了 100 次?它是否每 20 秒循环一次,它会根据我在#do some stuff 部分中的指示循环吗?
  • 非常感谢。完美运行
  • 不,有isn't 任何built in function,但您可以尝试clickloopassert 发生实际click 时的更改。并基于断言如果失败然后重试单击它是通过然后我们很好去。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签