【问题标题】:Get the element by title inside repeatable class in Selenium Webdriver in Python在 Python 中的 Selenium Webdriver 中的可重复类中按标题获取元素
【发布时间】:2021-10-20 13:11:51
【问题描述】:

我想在 selenium python 中点击一个包含类和标题的元素。

网页包含可重复的类,没有任何 id 但具有唯一名称。 一旦它在页面中加载,我想检测并单击此标题“PaymateSolutions”。 下面是html标签。我尝试了很多方法,但我最终遇到了错误。 仅供参考,我不能按类使用 find 元素,因为它们不是唯一的。

<div class="MuiGrid-root MuiGrid-item" title="PaymateSolutions">
<p class="MuiTypography-root jss5152 MuiTypography-body1">PaymateSolutions</p>
</div>

我尝试使用 XPATH 根据标题获取驱动程序元素的几种方法

方法一:-

wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH,"//class[@title='PaymateSolutions']")))

方法2:-

element2 = (WebDriverWait(driver, 30).until(
            EC.visibility_of_element_located((By.XPATH, "//p[@title='PaymateSolutions']")))

        )

方法3:-

element2 = (WebDriverWait(driver, 30).until(
            EC.visibility_of_element_located((By.XPATH, "//[@title='PaymateSolutions']")))

        )

有人可以帮忙吗?

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    对于方法 1 - titlediv 标签的属性。所以 Xpath 如下所示:

    //div[@title='PaymateSolutions']
    

    对于方法 2 - p 标签没有 title 属性。 PaymateSolutionsp 标签的text。 Xpath 应该是这样的:

    //p[text()='PaymateSolutions']
    

    对于方法 3 - xpath 中没有 Tag Name。 Xpath 将是:

    //*[@title='PaymateSolutions']
    Or
    //div[@title='PaymateSolutions']
    

    参考链接 - Link1, Link2

    我们可以像下面这样申请Explicit waits

    # Imports required for Explicit waits:
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver.get(url)
    
    wait = WebDriverWait(driver,30)
    payment_option = wait.until(EC.element_to_be_clickable((By.XPATH,"xpath for PaymateSolutions option")))
    payment_option.click()
    

    指向显式等待的链接 - Link

    【讨论】:

    • 工作就像一个魅力,你解释每个流程的方式真的很有帮助。非常感谢。
    • 需要一个小信息。你知道一种方法,一旦页面加载后点击元素,我们可以延迟读取所有 html 元素标签。我试图给出一个隐含的延迟,然后读取 page_source 但它不工作
    • @pythonNinja - 谢谢。我们可以应用显式等待来点击元素。但如果网站需要时间加载,请在driver.get(url) 或单击元素后应用一些time.sleep(10)。已重新更新答案。经历同样的事情。
    • time.sleep() 不推荐,但不确定网站功能。因此建议这样做。
    【解决方案2】:

    您一直在尝试的所有 XPath 似乎都有点错误。请使用以下 XPath:

    //div[@title='PaymateSolutions']//p[text()='PaymateSolutions']
    

    代码试用 1:

    time.sleep(5)
    driver.find_element_by_xpath("//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']").click()
    

    代码试用 2:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']"))).click()
    

    代码试用 3:

    time.sleep(5)
    button = driver.find_element_by_xpath("//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']")
    driver.execute_script("arguments[0].click();", button)
    

    代码试用 4:

    time.sleep(5)
    button = driver.find_element_by_xpath("//div[@title='PaymateSolutions']//p[text()='PaymateSolutions']")
    ActionChains(driver).move_to_element(button).click().perform()
    

    进口:

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

    【讨论】:

      猜你喜欢
      • 2017-02-14
      • 2017-08-05
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2021-10-14
      • 2015-12-06
      • 1970-01-01
      相关资源
      最近更新 更多