当前位置: 首页 > 知识库问答 >
问题:

如何捕获Selenium中动态的下拉按钮的正确xpath或元素

裴韬
2023-03-14

我一直试图选择一个下拉菜单,但我注意到它的XPath和ID每次都在变化,所以它可能是动态的。如何在这种类型的条件下捕获元素的正确路径?我们想把CSV放在下拉菜单上

<div class="form-group">
        <label>Report Type</label>
    <div>
    <select data-dom-uuid="" tabindex="-1" data-name="Report Type" data-input-id="attached_report_type" data-type="select" class="editor-input select2-hidden-accessible" aria-label="Report Type" aria-hidden="true">


            <option value="csv">CSV</option>
            <option selected="selected" value="db">db</option>
            <option value="pdf">PDF</option>


    </select><span class="select2 select2-container select2-container--db select2-container--below select2-container--open" dir="ltr" style="width: 100px;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="true" tabindex="11" aria-labelledby="select2-bka6-container" aria-owns="select2-bka6-results" aria-activedescendant="select2-bka6-result-vwdq-db"><span class="select2-selection__rendered" id="select2-bka6-container" title="db">db</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>

</div>
</div>

我们尝试过这些选择,但都不奏效,

#driver.find_element_by_xpath("/html/body/span/span").click()
    #driver.find_element_by_xpath("//*[@id=select2-7h5y-result-ycb2-csv]").click()
    #driver.find_element_by_id("id=select2-mrbe-container]").click()
    #driver.find_element_by_xpath("/html/body/section[3]/section[3]/section/form/div[1]/div/div/div[1]/section/div[3]/div[1]/div[1]/div[2]/div[6]/div[1]/div/select").click()
    select = Select(driver.find_element_by_xpath("//select[@data-input-id='attached_report_type']"))
    select.select_by_value("CSV").click()

共有2个答案

萧自珍
2023-03-14

正如您发布的HTML,我认为data name=“Report Type”是静态的,足以找到目标select元素,因为HTML标签中的元素也是相同的。应将以下定位器与显式等待一起使用:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)

>

element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "select[data-name='Report Type']")))

通过xpath

element = wait.until(EC.presence_of_element_located((By.XPATH, "//label[text()='Report Type']/following::select[@data-name='Report Type']")))

现在,您可以使用上述任何策略定位器来查找目标元素并使用选择

select = Select(element)
select.select_by_value("CSV").click()

希望能有帮助

杜经艺
2023-03-14

请使用ByTagName

driver.findElement(By.tagName("select"));
 类似资料:
  • 我不熟悉这个GUI脚本。。。。我的要求是单击下拉箭头, 因此,首先我需要破解XPATH,以便在此处输入“COUNTERS”下拉箭头的图像描述 使用绝对Xpath在此处输入图像描述,如下所示: 还包括相对路径: WebDriverWait(self.driver,70)。直到(EC.element可点击((By.XPATH,//i[@class='icon tree node arrow expan

  • 按钮超文本标记语言代码我将如何编写命令来单击按钮的xpath? Xpath是 是吗

  • 我一直试图使用硒网络驱动程序找到以下按钮: 我使用了css选择器、xpath和类,但似乎什么都不起作用(即使只是复制粘贴了inspector给出的一个。最近的一次是使用类_1ibtygfe定位div)。这是我尝试过的一切,我很绝望,我不明白为什么它找不到它,它一直向我抛出一个无此类元素异常。 这是airbnb网站,我正在尝试抓取他们的活动并点击查看更多按钮。谢谢你的帮助。

  • 我的AUT有不同的选项卡来测试和验证不同的WebElements,这些选项卡上有几乎相同的属性。我需要验证一个下拉搜索框,并验证下拉列表中的选项。我的脚本找不到这个下拉搜索框,而xpath在firebug中工作得非常好,可以识别搜索框。 显示的错误为: 请告诉我什么可能是错误的,以及为什么Selenium不能使用xpath定位元素,而它在xpath中运行良好。另外,如果我的xpath不正确,请告诉

  • 问题内容: Selenium RC中是否有办法从xpath获取ID? 如果我有xpath 我想获取与xpath相关的所有节点的ID 问题答案: 您可以通过运行javascript使用以下命令获得该代码: 当然,这取决于源语言,但这将是这样的(在perl中,未经测试): 我一直认为应该有一个更直接的方法来执行此操作,但是我还没有找到它! *根据注释进行 *编辑 ,for循环计数应从1开始并包含,而且