无法使用selenium.webdriver单击列表中的项目

2024-09-29 19:30:26 发布

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

我想按位置进一步筛选搜索结果。我只想要“美国”的结果,所以要单击“美国”,其顺序根据关键字的不同而变化。
以下代码不起作用。没有错误消息,但什么也没有发生。谢谢大家!

  browser.implicitly_wait(30)
  element = browser.find_element_by_xpath("//span[contains(text(),'United 
  States')]")

  browser.execute_script("arguments[0].click();", element)

enter image description here

如果成功,则“美国”也应出现在狭义By列表中:

enter image description here

The HTML source is as follows:

    <ul class="supplemental filters  " aria-labelledby="podfiltersbuttonlocation" data-id="location">
                <li class="preferred">
                    <h3 class="closable">
                        <span>
                            Select location by publications to display at the top of this list. <a 
    href="#" data-action="editpreferred">Edit Settings</a>
                        </span>
                        <button data-action="dismisspreferred" type="button" class="icon la- 
   CloseRemove">
                            <span role="tooltip" class="tooltip">Dismiss this message</span>
                        </button>
                    </h3>
                </li><li>
                    <input type="checkbox" data-id="pf0" data-value="International" id="_sp79k_pf0" 
    data-filtertype="location">
                    <label for="_sp79k_pf0" aria-label="">
                            <span>International</span>
                            <span class="count">83,428</span>
                    </label>
                </li>
                <li>
                    <input type="checkbox" data-id="pf1" data-value="United States" id="_sp79k_pf1" 
    data-filtertype="location">
                    <label for="_sp79k_pf1" aria-label="">
                            <span>United States</span>
                            <span class="count">31,153</span>
                    </label>
                </li>
                <li>
                    <input type="checkbox" data-id="pf2" data-value="Non-jurisdictional" id="_sp79k_pf2" 
    data-filtertype="location">
                    <label for="_sp79k_pf2" aria-label="">
                            <span>Non-jurisdictional</span>
                            <span class="count">10,936</span>
                    </label>
                </li>
                <li>
                    <input type="checkbox" data-id="pf3" data-value="California" id="_sp79k_pf3" data- 
    filtertype="location">
                    <label for="_sp79k_pf3" aria-label="">
                            <span>California</span>
                            <span class="count">6,785</span>
                    </label>
                </li>
                <li>
                    <input type="checkbox" data-id="pf4" data-value="Georgia" id="_sp79k_pf4" data- 
   filtertype="location">
                    <label for="_sp79k_pf4" aria-label="">
                            <span>Georgia</span>
                            <span class="count">2,814</span>
                    </label>
                </li>
                <li class="more">
                    <button data-action="moreless" class="icon la-ShowMore" type="button">
                        More
                    </button>
                </li>
                <li class="sel-multi">
                    <button type="button" data-action="selmulti" rel="modal" href="/modals/select- 
   Filters.shtml">Select multiple</button>
                </li>
            </ul>

Tags: idinputdatavaluetypebuttonlocationli
1条回答
网友
1楼 · 发布于 2024-09-29 19:30:26

点击checkbox诱导WebDriverWait()和element_to_be_clickable()和后面的Xpath

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//ul[@class='supplemental filters  ']//li[.//span[text()='United States']]/input[@type='checkbox']"))).click()

您需要导入以下库

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

相关问题 更多 >

    热门问题