【问题标题】:Click button but multiple classes单击按钮但有多个类
【发布时间】:2022-11-12 04:24:49
【问题描述】:

我可以点击“AKZEPTIEREN" 按钮使用 XPATH,如下所示:

WebDriverWait(driver, 15).until(expected_conditions.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div/button[2]'))).click()

为了使它更具动态性,我想使用一个类,所以这将是:

WebDriverWait(driver, 15).until(expected_conditions.element_to_be_clickable((By.CLASS_NAME, 'primary.accept.no-pop-execute'))).click()

但是“primary accept no-pop-execute”课程在网站上出现了两次。那么我该怎么做才能选择第一个呢?

页码:https://fremdgehen69.com

按钮:

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    您可以使用 xpath 表达式尝试以下代码,该表达式将选择单个元素并单击它,因为 click() 方法需要单个选择:

    WebDriverWait(driver, 15).until(expected_conditions.element_to_be_clickable((By.XPATH, '//*[@class="primary accept no-pop-execute" and contains(., "Akzeptieren")]'))).click()
    

    【讨论】:

    • 有什么作用。包含(?=)之后的意思
    • dot(.) 表示选择文本节点值,这里是 Akzeptieren
    • 谢谢 :))))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多