Python 我正在尝试获取用于下拉列表的XPath
我正在尝试获取一个下拉字段的XPath,我可以在我的Web驱动程序Python代码中使用它。 html中有两个下拉字段。我需要确定下拉列表,其中包含选项、人名、组织名称、地址等。 我想从下拉列表中选择地址值 我尝试了以下XPath:Python 我正在尝试获取用于下拉列表的XPath,python,selenium,xpath,selenium-webdriver,Python,Selenium,Xpath,Selenium Webdriver,我正在尝试获取一个下拉字段的XPath,我可以在我的Web驱动程序Python代码中使用它。 html中有两个下拉字段。我需要确定下拉列表,其中包含选项、人名、组织名称、地址等。 我想从下拉列表中选择地址值 我尝试了以下XPath: //select[@class="gwt-ListBox marginbelow"] 它标识两个下拉字段 <div class="clear"> <span class="gwt-InlineLabel marginbelow m
//select[@class="gwt-ListBox marginbelow"]
它标识两个下拉字段
<div class="clear">
<span class="gwt-InlineLabel marginbelow myinlineblock" style="width: 8em;">Type</span>
<select class="gwt-ListBox marginbelow" style="display: inline;">
<option value="Person name">Person name</option>
<option value="Organisation name">Organisation name</option>
<option value="Address">Address</option>
<option value="Date">Date</option>
<option value="Email">Email</option>
<option value="Phone">Phone</option>
<option value="Integer">Integer</option>
<option value="Numeric">Numeric</option>
<option value="String">String</option>
<option value="User-defined">User-defined</option>
</select>
</div>
<div class="clear">
<div class="clear">
<div class="clear">
<div style="display: none;" aria-hidden="true">
<div class="clear">
<span class="gwt-InlineLabel marginbelow myinlineblock" style="width: 8em;">TestDB</span>
<select class="gwt-ListBox marginbelow" style="display: inline;">
<option value="paf_uk15051">paf_uk15051</option>
</select>
</div>
</div>
谢谢你的建议。
Riaz尝试此操作以查找元素并直接单击它。这允许您绕过
Select
class的使用
//select/option[@value='Address']
您可以通过检查contains函数来尝试。 像这样:
//option[contains(text(),'Address')]
您可以这样尝试:
//select[@class="gwt-ListBox marginbelow"]/option[@value = 'Address']/text()
如果可以的话,我建议编辑HTML以包含ID,这样您就可以明确地分别标识两个选择框。如果你不能,这里有一个解决方案。我用您的html代码创建了一个JSFIDLE站点,这样您就可以执行脚本并亲自查看。您唯一需要更改的是路径和webdriver的选择:
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
driver = WebDriver("path_to_webdriver")
driver.get("http://jsfiddle.net/ksftLk9j/")
wait = WebDriverWait(driver, 20, 2)
wait.until(expected_conditions.frame_to_be_available_and_switch_to_it((By.NAME, 'result')))
element = driver.find_element_by_xpath('//select[@class="gwt-ListBox marginbelow"]/option[@value="Address"]')
element.click()
WebDriverWait和switch_to_frame语句是针对这种JSFIDLE情况的。这两条底线并非如此。你没有说你想对这个元素做什么,但我要做的是,假设你想选择它。普雷斯托。试试这个:
//select[contains(@class, 'ListBox')]/option[@value = 'Address']
因此,您的代码将是:
from selenium.webdriver.support import By
from selenium.webdriver.support.ui import select
data_objects_element_Type.Select(self.driver.find.element(By.XPATH, '//select[contains(@class, 'ListBox')]'))
data_objects_element_Type.select_by_visible_text(("Address"))
^我不懂python,但您需要方法smth。与selectByValue类似,您可以从根节点获取xpath。两个选择标记具有相同的类名,如果选择类名,它将高亮显示两个下拉列表。从起始节点发布您的HTML代码。我很高兴它对您有所帮助。
from selenium.webdriver.support import By
from selenium.webdriver.support.ui import select
data_objects_element_Type.Select(self.driver.find.element(By.XPATH, '//select[contains(@class, 'ListBox')]'))
data_objects_element_Type.select_by_visible_text(("Address"))