<div class="col-lg-6 col-sm-12 form-group drivers-form-group">
<label for="operating-system">Operating system</label>
<div class="select">
<select id="operating-system" class="w-100 form-control custom-select drivers-select">
<option value="BIOSA">BIOS</option>
<option value="NKLNA">NeoKylin</option>
<option value="UBT18">Ubuntu® 18.04 LTS</option>
<option value="LTSC1">Windows 10 64-Bit LTSC 2019</option>
<option value="W10GE">Windows 10 CMIT Government Edition</option>
<option value="WT64A" selected="">Windows 10, 64-bit</option>
<option value="W2021">Windows 11</option>
</select>
</div>
</div>
这是一个示例HTML文本,HTML是一个下拉列表,有7个选项。
现在,使用selenium的Basics,我可以获得Windows 1064位的Xpath并单击它,这样它就可以按照Windows 1064位更改内容。windows 10 64位的xpath为//*@id="operating-system"/option6
第二个Url样本
<div class="col-lg-6 col-sm-12 form-group drivers-form-group">
<label for="operating-system">Operating system</label>
<div class="select">
<select id="operating-system" class="w-100 form-control custom-select drivers-select">
<option value="BIOSA">BIOS</option>
<option value="NKLNA">NeoKylin</option>
<option value="WT64A" selected="">Windows 10, 64-bit</option>
<option value="LTSC1">Windows 10 64-Bit LTSC 2019</option>
<option value="W10GE">Windows 10 CMIT Government Edition</option>
<option value="DE0GE">Windows 8 CMIT Government Edition</option>
</select>
</div>
</div>
在下面提供的另一个示例代码中,Windows 10 64位位于第三位,
所以xpath将是//*@id=“操作系统”/option3
所以如果我在蟒蛇身上找到
driver.find_element_by_xpath('//*[@id="operating-system"]/option[6]')
我将获得第一个示例的Windows 10 64位数据和第二个示例的Windows 8 CMIT数据
现在,我想问的唯一问题是,在下拉列表中选择find 10,64位,并单击它,以查找通过Selenium或任何其他库循环的每个URL吗?
另外,如果我们可以尝试为两个示例数据选择一个相同的值代码,那么它可以工作。
发布于 2021-10-27 13:56:25
如前所述,可以使用Select
类从这个下拉列表中选择一个元素。
# Imports required:
from selenium.webdriver.support.select import Select
sel_option = Select(driver.find_element_by_id("operating-system"))
# Select by value
sel_option.select_by_value("LTSC1")
# Can also try with visible-text
sel_option.select_by_visible_text("Windows 10 64-Bit LTSC 2019")
更新:将所选选项作为属性selected
进行。可以使用相同的方法从元素中提取文本。
selected_option = driver.find_element_by_xpath("//select[@id='operating-system']/option[@selected]")
print(selected_option.text)
Or
print(selected_option.get_attribute("innerText"))
https://stackoverflow.com/questions/69739662
复制相似问题