我试图让XPath下拉

时间:2015-07-07 13:32:44

标签: python selenium xpath selenium-webdriver

我正在尝试将XPath用于下拉字段,我可以在我的Webdriver,Python代码中使用它。 html中有两个下拉字段。我需要确定下拉列表,其中包含选项,人员姓名,组织名称,地址等。 我想从下拉列表中选择地址值。

我尝试过以下XPath:

//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>

然后我会像这样使用XPath,例如

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[@class="gwt-ListBox marginbelow"]'))
data_objects_element_Type.select_by_visible_text(("Address"))

感谢您的建议。 里亚兹

5 个答案:

答案 0 :(得分:1)

您可以尝试检查包含功能。 像这样:

 //option[contains(text(),'Address')]

答案 1 :(得分:1)

如果可以,我建议您修改HTML以包含ID,以便您可以单独识别两个选择框。如果你不能,这是一个解决方案。我用你的html代码制作了一个jsfiddle网站,这样你就可以自己执行脚本了。你唯一需要改变的是路径和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-statements特定于此jsfiddle-case。两条底线不是。你没有说明你想对这个元素做什么,但我会想出去,并假设你想选择它。似的。

答案 2 :(得分:0)

尝试此操作以查找元素并直接单击它。这允许您绕过Select

的使用
//select/option[@value='Address']

答案 3 :(得分:0)

你可以这样试试:

  //select[@class="gwt-ListBox marginbelow"]/option[@value = 'Address']/text()

答案 4 :(得分:0)

试试这个:

//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