【问题标题】:Clicking on Javascript tab using Selenium and Python without unique class id or element name使用 Selenium 和 Python 在没有唯一类 ID 或元素名称的情况下单击 Javascript 选项卡
【发布时间】:2020-02-29 21:05:36
【问题描述】:

我有这个 HTML 元素代码,我目前正在努力想用它来单击显示问题的选项卡。由于“问题”没有唯一的类名或元素 ID,我无法弄清楚如何发送 Click()。

我尝试检查 z-index 是否可以用作索引(假设)并在代码行下方使用

browser.switch_to_frame(a[3])

但看来我错了。

HTML代码如下

<div class="TabsViewPort" style="position: relative; overflow: hidden; width: 896px; height: 22px; float: left;">
<div style="overflow: visible; float: left; width: 897px; top: 0px; left: 0px;">
<dl class="OuterOuterTab">
<dd class="OuterTab" artabid="955000038" arwindowid="0" style="top: 1px; z-index: 1; left: 0px; visibility: inherit; display: block;"><span class="TabLeftRounded">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn f1" style="color:#000000;">My&nbsp;Profile</a>
</span>
<span class="TabRight">&nbsp;</span>
</dd>
<dd class="OuterTabSelected" artabid="600000203" arwindowid="0" style="top: 1px; z-index: 3; left: 63px; visibility: inherit; display: block;"><span class="TabLeft">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn f1">Approval</a>
</span>
<span class="TabRight">&nbsp;</span>
</dd>
<dd class="OuterTab" artabid="536870915" arwindowid="0" style="top: 1px; z-index: 1; left: 409px; visibility: inherit; display: block;"><span class="TabLeft">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn f1">Problem</a>
</span>
<span class="TabRight">&nbsp;</span>
</dd>
</dl>
</div>
</div>

【问题讨论】:

    标签: python selenium xpath webdriverwait xpath-1.0


    【解决方案1】:

    文本为 Problem 的元素是启用了 JavaScript 的元素,因此要在元素上使用 click(),您必须为 元素诱导 WebDriverWait可点击,您可以使用以下任一解决方案:

    • 使用XPATH答:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='TabsViewPort']//dl[@class='OuterOuterTab']//dd[@class='OuterTab']//a[@class='btn f1' and text()='Problem']"))).click()
      
    • 使用XPATHB(缩写):

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn f1' and text()='Problem']"))).click()
      
    • 注意:您必须添加以下导入:

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

    【讨论】:

    • 我是一个从未尝试过 XPath 的菜鸟,但这给了我一个开始。谢谢!
    【解决方案2】:

    如果元素存在于 iframe 中,那么您需要先切换到 iframe 才能访问该元素。 您可以使用以下方法frame_to_be_available_and_switch_to_it()

    按定位器 ID

    WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"id of the iframe")))
    

    按定位器名称

    WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"name of the iframe")))
    

    切换到 iframe 后,您可以使用以下xpath 访问该元素

    点击元素 Induce WebDriverWaitelement_to_be_clickable()

    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[@class='Tab']//a[text()='Problem']"))).click()
    

    您需要导入以下代码才能执行上述代码。

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

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      请在 xpath 下方找到以单击第三个 TAB

      (//span[@class="Tab"])[3]/a
      

      【讨论】:

        猜你喜欢
        • 2021-11-16
        • 1970-01-01
        • 2021-04-24
        • 1970-01-01
        • 2018-09-04
        • 2021-05-05
        • 1970-01-01
        • 1970-01-01
        • 2020-07-21
        相关资源
        最近更新 更多