如何使用Selenium和Python单击启用GWT的元素 [英] How to click on GWT enabled elements using Selenium and Python
问题描述
我正在以Selenium的身份从事新手和开发人员的工作. 我尝试解决此XPath,但没有结果,这就是我寻求帮助的原因.
I'm working with Selenium as a newbie and also as a newbie developer. I try to solve this XPath but with no results that is why I'm looking for help.
因此,我想单击具有动态ID的复选框,但根据tittle ="Viewers"
So I want to click in the checkbox which has a dynamic id but it is not that easy to find this checkbox based on tittle="Viewers"
请注意,有一个完整的复选框列表,其名称位于div中的复选框的右边,我想将其包括在测试中.
Please notice there is a whole list of checkboxes with names on right from a checkbox in div which I want to include in my tests.
HTML:
<span class="row-selection"><input type="checkbox" value="on" id="gwt-uid-2215" tabindex="0"><label for="gwt-uid-2215"></label></span> <div class="row-label" title="Viewers">Viewers</div>
快照;
推荐答案
所需元素是 GWT 启用了元素,因此单击该元素必须引起 WebDriverWait 作为element_to_be_clickable()
,您可以使用以下任一基于xpath 的定位器策略:
The desired element is a GWT 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 xpath based Locator Strategy:
-
使用基于 title 属性的 xpath :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@title='Viewers']//preceding::span[1]//label"))).click()
使用基于 innerHTML 的 xpath :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Viewers']//preceding::span[1]//label"))).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
这篇关于如何使用Selenium和Python单击启用GWT的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!