如何通过Python使用Selenium来定位按钮元素 [英] How to locate the button element using Selenium through Python

查看:1445
本文介绍了如何通过Python使用Selenium来定位按钮元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找元素,然后单击按钮不立即".我已经尝试过使用xs的css_selector,但是我根本找不到正确的方法.

I'm trying to find the element and click for the button "Not Now". I've tried with with css_selector, xpath, but I"m unable at all to find the proper way.

HTML:

推荐答案

要定位并在元素上以不立即显示作为文本的click(),您可以使用以下

To locate and click() on the element with text as Not Now you can use the following Locator Strategy:

  • 使用xpath:

driver.find_element_by_xpath("//button[text()='Not Now']").click()

但是,该元素对我而言似乎是动态的,因此您需要为element_to_be_clickable()引入 WebDriverWait ,并且可以使用以下任意一种 Locator Strategy :

However, the element looks dynamic to me so you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:

  • 使用XPATH:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div//button[text()='Not Now']"))).click()

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

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

  • 您可以在以下位置找到一些相关的讨论:

    You can find a couple of relevant discussions in:

    • What does contains(., 'some text') refers to within xpath used in Selenium
    • While fetching all links,Ignore logout link from the loop and continue navigation in selenium java
    • How does dot(.) in xpath to take multiple form in identifying an element and matching a text

    这篇关于如何通过Python使用Selenium来定位按钮元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆