从下拉框中收集所选值的文本,点击后不改变元素上的任何内容。

huangapple go评论121阅读模式
英文:

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">&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>

当选择另一个选项时,可视文本会更改,但元素中的任何内容都不会更改,包括仍然显示第一个选项被选中:

从下拉框中收集所选值的文本,点击后不改变元素上的任何内容。

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

在元素保持不变的情况下,我如何获取框的新值?

我使用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:

从下拉框中收集所选值的文本,点击后不改变元素上的任何内容。

&lt;table class=&quot;table compare section-dropdowns&quot;&gt;
  &lt;thead&gt;
    &lt;tr class=&quot;sub-head dropdown-head&quot;&gt;
      &lt;th class=&quot;col1&quot;&gt;&amp;nbsp;&lt;/th&gt;
      &lt;th&gt;&lt;div data-active-dropdown-id=&quot;&quot; class=&quot;active-dropdown&quot;&gt;
  &lt;select name=&quot;competition_id&quot;&gt;
    
      
      &lt;option value=&quot;89&quot; selected=&quot;selected&quot;&gt;Serie B - 2023&lt;/option&gt;
      
      &lt;option value=&quot;239&quot;&gt;Paulista A1 - 2023&lt;/option&gt;
      
      &lt;option value=&quot;231&quot;&gt;Copa do Brasil - 2023&lt;/option&gt;
    
  &lt;/select&gt;
&lt;/div&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
&lt;/table&gt;

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:

从下拉框中收集所选值的文本,点击后不改变元素上的任何内容。

&lt;table class=&quot;table compare section-dropdowns&quot;&gt;
  &lt;thead&gt;
    &lt;tr class=&quot;sub-head dropdown-head&quot;&gt;
      &lt;th class=&quot;col1&quot;&gt;&amp;nbsp;&lt;/th&gt;
      &lt;th&gt;&lt;div data-active-dropdown-id=&quot;&quot; class=&quot;active-dropdown&quot;&gt;
  &lt;select name=&quot;competition_id&quot;&gt;
    
      
      &lt;option value=&quot;89&quot; selected=&quot;selected&quot;&gt;Serie B - 2023&lt;/option&gt;
      
      &lt;option value=&quot;239&quot;&gt;Paulista A1 - 2023&lt;/option&gt;
      
      &lt;option value=&quot;231&quot;&gt;Copa do Brasil - 2023&lt;/option&gt;
    
  &lt;/select&gt;
&lt;/div&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
&lt;/table&gt;

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, &#39;html.parser&#39;)
season_opt = soup.select(&#39;table.section-dropdowns select[name=&quot;competition_id&quot;]&#39;)[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, &quot;//select[@name=&#39;competition_id&#39;]&quot;)
options = dropdown.find_elements(By.XPATH, &#39;./option&#39;)
for value in [&#39;89&#39;,&#39;239&#39;,&#39;231&#39;]:
    Select(dropdown).select_by_value(value)
    print( [o.text for o in options if o.is_selected()] )

output

[&#39;Serie B - 2023&#39;]
[&#39;Paulista A1 - 2023&#39;]
[&#39;Copa do Brasil - 2023&#39;]

huangapple
  • 本文由 发表于 2023年2月24日 02:07:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定