gpt4 book ai didi

python - 识别下拉列表中的值时出现问题(Selenium Python)

转载 作者:行者123 更新时间:2023-12-01 03:53:36 25 4
gpt4 key购买 nike

我正在使用 Selenium (Python) 从下拉列表中选择一个值,但遇到了一些问题。

当我使用 Selenium IDE 时,它将下拉列表识别为按钮,如下所示:

    driver.find_element_by_xpath("(//button[@type='button'])[2]")

然后我尝试使用以下方法从列表中选择一个值:

    Select(driver.find_element_by_xpath("(//button[@type='button'])[2]")).select_by_visible_text("Africa/Juba")

这不起作用,因为它说“选择仅适用于元素,不适用于按钮。

当我检查元素时,我看不到任何可用于识别下拉菜单的有用信息(这只是其中的一部分):

<select selectpicker="" ng-options="timezone for timezone in model.timezones" ng-model="newAccount.timeZoneId" class="form-control ng-pristine ng-untouched ng-valid bs-select-hidden" data-live-search="true">  <option value="0" label="Africa/Abidjan">Africa/Abidjan</option><option value="1" label="Africa/Accra">Africa/Accra</option>  <option value="2" label="Africa/Addis_Ababa">Africa/Addis_Ababa</option>  <option value="3</option></select>  

还有其他方法可以从下拉列表中选择值吗?

最佳答案

您通过 Xpath 获得的元素是 button 。您不能使用 Select与按钮。您想获得select元素。试试这个:

Select(driver.find_element_by_xpath("//select[@ng-model='newAccount.timeZoneId']")).select_by_visible_text("Africa/Juba")

编辑你遇到的问题是有道理的。在 HTML你分享没有option包含文本 Africa/Juba 的元素。如果你愿意改变 Africa/JubaAfrica/Algiers它应该可以正常工作。在评论中您提到了这个元素:<span class="text">Africa/Juba</span> 。您的 HTML 中缺少此内容例子。然而span里面 select不应该工作。您可以使用 Select处理options时的类.

关于python - 识别下拉列表中的值时出现问题(Selenium Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com