如何从下面的htm中选择弹出式下拉框中的值

2024-10-04 11:23:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有弹出警告与下拉值出现在弹出窗口。这些值将基于以前的选择列表。有谁能帮我用python selenium模块从这个下拉列表中选择值吗。你知道吗

<div class="none">
    <div id="selectfavcolor">
        <h3 class="popupTitle">Select your favourite color</h3>

        <div class="clearFix pdLR15">
            <!--newSelectBox start-->
            <div class="newSelectBox">
                <div class="dd-select-main clearFix">
                    <div id="myDropdown"></div>
                    <label id='SlctColorError' class='dispNone SlctErrors error'></label>
                </div>

                <div class="pdTB15 alRgt">
                    <a href="javascript:;" id="savecolor" class="darkYellowBtn">Save</a>
                </div>
            </div>
            <!--newSelectBox end-->
        </div>
    </div>
</div>

我试过这样。但它不起作用。你知道吗

select_make = driver.find_element_by_id('myDropdown')
for option in select_make.find_elements_by_tag_name('SlctColorError'):
    if option.text == 'Blue':
        option.click() # select() in earlier versions of webdriver
        break

Tags: divid列表makebyfindselectlabel
1条回答
网友
1楼 · 发布于 2024-10-04 11:23:09

假设在执行任何其他操作时,选项Blue出现在屏幕上,
更好的方法是等到它出现在屏幕上并且可以单击,然后单击它。

一个例子(代码是用Java编写的,因为我不知道Phyton,但我相信您会设法将其转换为Phyton):

final By blueOption = By.xpath( "//*[ text() = 'Blue' ]" ); 

/* wait max. 10 seconds then throw an exception if the option has't appeared */
WebDriverWait wait = new WebDriverWait(driver, 10); 

wait.until( ExpectedConditions.elementToBeClickable( blueOption )).click();

相关问题 更多 >