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

我有这个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>
ABCwoaidianlusheji 回答:在没有唯一类ID或元素名称的情况下,使用Selenium和Python单击Javascript选项卡

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

通过定位器ID

frame_to_be_available_and_switch_to_it

OR

按定位器名称

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

一旦您切换到iframe,就可以使用以下WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"name of the iframe")))

访问该元素

要单击元素引发xpathWebDriverWait()

element_to_be_clickable

您需要导入以下内容以执行上述代码。

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

希望这会有所帮助。

,

文本为问题的元素是启用了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,"//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下找到第三个TAB

(//span[@class="Tab"])[3]/a
本文链接:https://www.f2er.com/3166075.html

大家都在问