单击使用Selenium和Python的Javascript选项卡,没有唯一的类ID或元素名称

问题描述 投票:1回答:3

我有这个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
3个回答
1
投票

文本为问题的元素是启用了JavaScript的元素,因此对于该元素上的click(),您必须诱导WebDriverWait,以便使[元素可单击],并且可以使用以下任一解决方案:

  • 使用XPATH A:

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()

  • 使用XPATH B(缩短):
  • 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

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

    0
    投票
    请在xpath下找到以单击第三个TAB
    © www.soinside.com 2019 - 2024. All rights reserved.