如何使用Selenium Python从Tableau报表网页的下拉菜单中选择选项

h43kikqp  于 2023-02-04  发布在  Python
关注(0)|答案(1)|浏览(198)

当我试图通过ID或类名查找元素时,Python抛出错误。请帮助查找哪些代码将有助于选择特定下拉列表的数据
超文本:

</div>
<div class="clslblLevelStatus">Level Status</div>
<div class="clsdrpstatus">
    <select id="levelstatusDropdownlist" class="chzn-select-deselect chzn-done" style="width: 200px; display: none;" tabindex="-1">
        <option value=""></option>
    <option value="">--All Statuses--</option><option value="0|1000">Unassigned</option><option value="1|15">L1 - Work In Progress</option><option value="1|14">L1 - Pending</option><option value="1|2">L1 - Hold</option><option value="1|1">L1 - Completed</option><option value="1|3">L1 - Invalid</option><option value="2|15">L2 - Work In Progress</option><option value="2|14">L2 - Pending</option><option value="2|2">L2 - Hold</option><option value="2|3">L2 - Invalid</option><option value="3|15">L3 - Work In Progress</option><option value="3|14">L3 - Pending</option><option value="3|2">L3 - Hold</option><option value="3|3">L3 - Invalid</option><option value="0|1001">Completed</option></select><div id="levelstatusDropdownlist_chzn" class="chzn-container chzn-container-single" style="width: 200px;" title=""><a href="javascript:void(0)" class="chzn-single chzn-default" tabindex="-1"><span>Select an Option</span><div><b></b></div></a><div class="chzn-drop" style="left: -9000px; width: 198.4px; top: 23px;"><div class="chzn-search"><input type="text" autocomplete="off" tabindex="2" style="width: 163.8px;"></div><ul class="chzn-results"><li id="levelstatusDropdownlist_chzn_o_1" class="active-result" style="">--All Statuses--</li><li id="levelstatusDropdownlist_chzn_o_2" class="active-result" style="">Unassigned</li><li id="levelstatusDropdownlist_chzn_o_3" class="active-result" style="">L1 - Work In Progress</li><li id="levelstatusDropdownlist_chzn_o_4" class="active-result" style="">L1 - Pending</li><li id="levelstatusDropdownlist_chzn_o_5" class="active-result" style="">L1 - Hold</li><li id="levelstatusDropdownlist_chzn_o_6" class="active-result" style="">L1 - Completed</li><li id="levelstatusDropdownlist_chzn_o_7" class="active-result" style="">L1 - Invalid</li><li id="levelstatusDropdownlist_chzn_o_8" class="active-result" style="">L2 - Work In Progress</li><li id="levelstatusDropdownlist_chzn_o_9" class="active-result" style="">L2 - Pending</li><li id="levelstatusDropdownlist_chzn_o_10" class="active-result" style="">L2 - Hold</li><li id="levelstatusDropdownlist_chzn_o_11" class="active-result" style="">L2 - Invalid</li><li id="levelstatusDropdownlist_chzn_o_12" class="active-result" style="">L3 - Work In Progress</li><li id="levelstatusDropdownlist_chzn_o_13" class="active-result" style="">L3 - Pending</li><li id="levelstatusDropdownlist_chzn_o_14" class="active-result" style="">L3 - Hold</li><li id="levelstatusDropdownlist_chzn_o_15" class="active-result" style="">L3 - Invalid</li><li id="levelstatusDropdownlist_chzn_o_16" class="active-result" style="">Completed</li></ul></div></div>

我试过下面的代码

Queue_status = Select(browser.find_element(By.ID,'levelstatusDropdownlist'))
Queue_status.select_by_visible_text("--All Statuses--")

Queue_status = browser.find_element(By.ID,'levelstatusDropdownlist_chzn'))
Queue_status.click()
6psbrbz9

6psbrbz91#

<select>具有属性 * style="width: 200px; display: none;" *,因此不能使用<select>标记。
溶液
要选择一个选项,需要为element_to_be_clickable()引入WebDriverWait,可以使用以下locator strategies

  • 代码块:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a//span[text()='Select an Option']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='chzn-drop']//ul[@class='chzn-results']//li[text()='L2 - Work In Progress']"))).click()
      • 注意**:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

相关问题