在没有唯一类ID或元素名称的情况下,使用Selenium和Python单击Javascript选项卡 [英] Clicking on Javascript tab using Selenium and Python without unique class id or element name
问题描述
我有这个HTML元素代码,目前我正在设法找出它来使用它来单击显示问题"的选项卡.由于问题"没有唯一的类名或元素ID,因此我无法确定如何发送Click().
I have this HTML element code which I am currently struggling to figure out to use it for clicking on the tab that says Problem. As the "Problem" doesnt have a unique classname or element ID, I am unable to figure how to send a Click().
我尝试检查z-index是否可以用作索引(假定)并在代码行下方使用
I have tried to check if z-index can be used as index(assumed) and used below line of code
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"> </span>
<span class="Tab"><a href="javascript:" class="btn f1" style="color:#000000;">My Profile</a>
</span>
<span class="TabRight"> </span>
</dd>
<dd class="OuterTabSelected" artabid="600000203" arwindowid="0" style="top: 1px; z-index: 3; left: 63px; visibility: inherit; display: block;"><span class="TabLeft"> </span>
<span class="Tab"><a href="javascript:" class="btn f1">Approval</a>
</span>
<span class="TabRight"> </span>
</dd>
<dd class="OuterTab" artabid="536870915" arwindowid="0" style="top: 1px; z-index: 1; left: 409px; visibility: inherit; display: block;"><span class="TabLeft"> </span>
<span class="Tab"><a href="javascript:" class="btn f1">Problem</a>
</span>
<span class="TabRight"> </span>
</dd>
</dl>
</div>
</div>
推荐答案
文本为问题的元素是启用了JavaScript 的元素,因此要在该元素上click()
,必须诱使 WebDriverWait 使元素可点击,并且您可以使用任一以下解决方案:
The element with text as Problem is a JavaScript enabled element so to click()
on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
-
使用
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(缩短):
Using XPATH
B (shortened):
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn f1' and text()='Problem']"))).click()
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
这篇关于在没有唯一类ID或元素名称的情况下,使用Selenium和Python单击Javascript选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!