【问题标题】:How to find the Xpath of the element as per the html through Selenium and Python如何通过 Selenium 和 Python 根据 html 查找元素的 Xpath
【发布时间】:2018-07-30 09:02:14
【问题描述】:

在 python 中尝试过类似的操作,但我想使用 Selenium 点击交叉。

driver.find_element_by_xpath("//span[contains(@onclick, 'parent.$WZRK_WR.closeIframe('60005','intentPreview');')]").click()

【问题讨论】:

    标签: python selenium selenium-webdriver xpath webdriver


    【解决方案1】:

    根据您共享的 HTML,单击描述为 X 的元素,首先您需要在切换到<iframe> 并再次诱导 WebDriverWait 使所需的元素可点击,您可以使用以下解决方案:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='wiz-iframe-intent']")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='CT_Interstitial']//span[@class='CT_InterstitialClose']"))).click()
    

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

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

    【讨论】:

    • wiz-iframe-intent 在 OP 共享的屏幕截图中可见吗?
    • @cruisepandey 粗略的 frameID 几乎是可见的。
    • 好的。从我的那张屏幕截图来看,它是不可见的。
    • @cruisepandey 也许作为社区成员,你、我和每个人都看到了相同的屏幕截图 :)
    【解决方案2】:
    1. 打开下面的 URL 并点击添加到 Chrome 按钮。

    Xpath Helper-https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en

    1. 一旦插件添加到 Chrome。打开您的应用程序/网页,然后按 SHIFT+CTRL+X。顶部会出现黑色窗口,然后您可以使用 xpath。

    如果 xpath 无效 - 那么您将收到 xpath 错误,如果找不到匹配项,则会显示 NULL。

    注意-要检查元素属性,您仍然可以使用 F12,它只是所有浏览器的默认检查元素,然后检查属性并创建自己的 xpath 并尝试。

    Xpath 语法 // tagname[@attribute-name=’value1′] 如果您不确定标签名称,那么不用担心,您也可以尝试使用 *

    //*[@attribute-name='value1']
    

    【讨论】:

      【解决方案3】:

      您正在处理 iframe。请按照以下步骤操作:

      1. 您需要将控制权切换到 iframe。

      2. 然后执行您的操作(在本例中为“单击关闭”)。

      3. 将控件切换回默认框架。

      【讨论】:

        【解决方案4】:

        你可以试试这个css_selector

        div.CT_InterstitialContents+span.CT_InterstitialClose[onclick]  
        

        Xpath 将是:

        //div[@class='CT_InterstitialContents']/following-sibling::span[@class='CT_InterstitialClose' and onclick]
        

        【讨论】:

          【解决方案5】:

          尝试使用这个xPath:

          //div[@id = 'contentDiv']/div/div/span[@class = 'CT_InterstitialClose']
          

          代码:

          close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id = 'contentDiv']/div/div/span[@class = 'CT_InterstitialClose']")))
          close_btn.click()
          

          进口:

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

          解释:

          WebDriverWait 用于等待元素可点击,然后才点击它。在此示例中,WebDriverWait 将等待至少 10 秒,直到元素可点击。

          PS: 正如我在屏幕截图中看到的那样,您的元素可能在 iframe 中。这意味着您必须先切换到此iframe,才能与其中的元素进行交互。它的代码示例如下:

          WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "XPATH_TO_FRAME")))
          # do stuff
          close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id = 'contentDiv']/div/div/span[@class = 'CT_InterstitialClose']")))
          close_btn.click()
          # switch back to default content
          driver.switch_to.default_content()
          

          【讨论】:

            【解决方案6】:

            您可以使用 span 的类名编写 xpath。请参考下面的示例。

            //span[@class='amountCharged']
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-03-12
              • 1970-01-01
              • 2018-08-28
              • 2020-11-29
              • 1970-01-01
              • 1970-01-01
              • 2015-11-23
              • 1970-01-01
              相关资源
              最近更新 更多