【问题标题】:Selenium Python: Get Xpath value selectedSelenium Python:获取选定的 Xpath 值
【发布时间】:2021-10-13 15:47:03
【问题描述】:

我期待知道在 selenium 中找到元素后选择的 XPATH 值是什么。我想知道,因为我会根据情况采取不同的行动,如果没有找到任何元素,我会引发异常

使用以下代码,我可以找到 3 个案例中的任何一个:

get_main_1 = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located((
                By.XPATH, '//div[@role="article"] '
                          '| //span[@role="presentation"] '
                          '| //div[@aria-label="Video"] '
                          '')))[0]

如果我得到//div[@role="article"] 我会做一些动作,如果我得到//span[@role="presentation"] 其他动作,等等......

我怎样才能知道我发现了 3 个案例中的哪一个?

【问题讨论】:

  • 请分享指向该页面的链接或至少包含选定元素和未选定元素的相关元素的 HTML

标签: python python-3.x selenium selenium-webdriver


【解决方案1】:

要知道返回了什么元素,您可以执行以下操作:

get_main_1 = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located((
                By.XPATH, '//div[@role="article"] '
                          '| //span[@role="presentation"] '
                          '| //div[@aria-label="Video"] '
                          '')))[0]
role = get_main_1.get_attribute("role")
if "article" in role:
    #first element returned actions
elif "presentation" in role:
    #second element returned actions
else:
    #third element returned actions

【讨论】:

    猜你喜欢
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2021-11-23
    • 2019-04-27
    • 1970-01-01
    相关资源
    最近更新 更多