英文:
Collect selected value text from a dropdown box that doesn't change anything on the element upon click
问题
我使用Selenium WebDriver的Firefox打开此页面。
这里有一个下拉框:
<table class="table compare section-dropdowns">
<thead>
<tr class="sub-head dropdown-head">
<th class="col1"> </th>
<th><div data-active-dropdown-id="" class="active-dropdown">
<select name="competition_id">
<option value="89" selected="selected">Serie B - 2023</option>
<option value="239">Paulista A1 - 2023</option>
<option value="231">Copa do Brasil - 2023</option>
</select>
</div></th>
</tr>
</thead>
</table>
当选择另一个选项时,可视文本会更改,但元素中的任何内容都不会更改,包括仍然显示第一个选项被选中:
<table class="table compare section-dropdowns">
<thead>
<tr class="sub-head dropdown-head">
<th class="col1"> </th>
<th><div data-active-dropdown-id="" class="active-dropdown">
<select name="competition_id">
<option value="89" selected="selected">Serie B - 2023</option>
<option value="239">Paulista A1 - 2023</option>
<option value="231">Copa do Brasil - 2023</option>
</select>
</div></th>
</tr>
</thead>
</table>
在元素保持不变的情况下,我如何获取框的新值?
我使用WebDriver和Beautifulsoup测试了几种方法,但没有一个正确地获取值,包括获取文本而不设置任何其他细节:
soup = BeautifulSoup(driver.page_source, 'html.parser')
season_opt = soup.select('table.section-dropdowns select[name="competition_id"]')[0]
season = season_opt.text.strip()
英文:
I open this page with selenium webdrive Firefox.
Here there is a dropdown box:
<table class="table compare section-dropdowns">
<thead>
<tr class="sub-head dropdown-head">
<th class="col1">&nbsp;</th>
<th><div data-active-dropdown-id="" class="active-dropdown">
<select name="competition_id">
<option value="89" selected="selected">Serie B - 2023</option>
<option value="239">Paulista A1 - 2023</option>
<option value="231">Copa do Brasil - 2023</option>
</select>
</div></th>
</tr>
</thead>
</table>
That when selecting another option, the visual text changes, but nothing in the element changes, including it keeps saying that the first option is selected:
<table class="table compare section-dropdowns">
<thead>
<tr class="sub-head dropdown-head">
<th class="col1">&nbsp;</th>
<th><div data-active-dropdown-id="" class="active-dropdown">
<select name="competition_id">
<option value="89" selected="selected">Serie B - 2023</option>
<option value="239">Paulista A1 - 2023</option>
<option value="231">Copa do Brasil - 2023</option>
</select>
</div></th>
</tr>
</thead>
</table>
How can I get the new value of the box after selecting it while the element remains unchanged?
I tested several shapes with the webdriver and with Beautifulsoup, but none delivered correctly, including getting the text without setting any other details:
soup = BeautifulSoup(driver.page_source, 'html.parser')
season_opt = soup.select('table.section-dropdowns select[name="competition_id"]')[0]
season = season_opt.text.strip()
答案1
得分: 1
方法 is_selected()
如果元素被选中则返回 true,否则返回 false。以下代码遍历所有的值,选择相应的选项,并打印所选的选项。
from selenium.webdriver.support.ui import Select
dropdown = driver.find_element(By.XPATH, "//select[@name='competition_id']")
options = dropdown.find_elements(By.XPATH, './option')
for value in ['89', '239', '231']:
Select(dropdown).select_by_value(value)
print([o.text for o in options if o.is_selected()])
输出
['Serie B - 2023']
['Paulista A1 - 2023']
['Copa do Brasil - 2023']
英文:
The method is_selected()
returns true if an element is selected, false otherwise. Following code loops through all the values, select the corresponding option and prints the selected option.
from selenium.webdriver.support.ui import Select
dropdown = driver.find_element(By.XPATH, "//select[@name='competition_id']")
options = dropdown.find_elements(By.XPATH, './option')
for value in ['89','239','231']:
Select(dropdown).select_by_value(value)
print( [o.text for o in options if o.is_selected()] )
output
['Serie B - 2023']
['Paulista A1 - 2023']
['Copa do Brasil - 2023']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论