用Selenium选择一个选项(Python)

时间:2017-03-13 13:53:48

标签: python selenium safari web-scraping

我正试图抓一个网站:Arval.fr,我在创建搜索树时遇到了麻烦。 表格有3种选择:型号,汽车制造商和汽车型号,它们根据之前选择的内容进行更新。

我正在使用selenium来获取所有可能的选项,并且我设法从第一个框中正确读取了所有选项,但我无法选择一个选项以便下一个选项更新。

这是我到目前为止所做的:

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Safari()
driver.get("http://www.arval.fr")

select = Select(driver.find_element_by_id(vehicleTypeRef))
select.options[1].click()

我希望在vehicleTypeRef id的框中选择第二个选项,但没有任何反应。我已使用all_selected_options进行了检查,该框仍设置为默认选项...

我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

有选择选项的方法

select = Select(driver.find_element_by_id(vehicleTypeRef))
select.select_by_index(1)

有关更多方法,请查看docs

  

修改

手动尝试后,问题出在网站上。选择其他选项不会更改selected="selected"属性

enter image description here

选择其他选项后

enter image description here

答案 1 :(得分:0)

试试这段代码,

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel HorizontalAlignment="Center">
                    <TextBlock Foreground="#2c7cb8" FontSize="14" 
                     Text="{Binding Name }" />
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
</ListView.GroupStyle>

这将根据第一个下拉列表中的所有三个值的选择以及基于它的模型为您提供输出。

示例输出:

CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(
                       chatList.ItemsSource); 
view.GroupDescriptions.Add(new PropertyGroupDescription("Date"));

希望这就是您所寻找的。如果代码中的任何内容不清楚,请告诉我