使用Selenium和Python从下拉列表中选择
在Selenium和Python的帮助下。我想抓取一个有嵌套下拉菜单的网页。我只发布下面的嵌套部分:使用Selenium和Python从下拉列表中选择,python,selenium,web-crawler,Python,Selenium,Web Crawler,在Selenium和Python的帮助下。我想抓取一个有嵌套下拉菜单的网页。我只发布下面的嵌套部分: <div class="dropDown active" data-dropdown-block="FOOTBALL_COMPSEASON" data-dropdown-default="All Seasons"> <div class="label" id="dd-FOOTBALL_COMPSEASON">Filter by Season</div>
<div class="dropDown active" data-dropdown-block="FOOTBALL_COMPSEASON" data-dropdown-default="All Seasons">
<div class="label" id="dd-FOOTBALL_COMPSEASON">Filter by Season</div>
<div class="current" data-dropdown-current="FOOTBALL_COMPSEASON" role="button" tabindex="0" aria-expanded="false" aria-labelledby="dd-FOOTBALL_COMPSEASON" data-listen-keypress="true" data-listen-click="true">
2018/19
</div>
<ul class="dropdownList" data-dropdown-list="FOOTBALL_COMPSEASON" role="listbox" aria-labelledby="dd-FOOTBALL_COMPSEASON" data-listen-keypress="true" data-listen-click="true">
<li role="option" tabindex="0" data-option-name="All Seasons" data-option-id="-1" data-option-index="-1">
All Seasons
</li>
<li role="option" tabindex="0" data-option-name="2018/19" data-option-id="210" data-option-index="0">
2018/19
</li>
<li role="option" tabindex="0" data-option-name="2017/18" data-option-id="79" data-option-index="1">
2017/18
</li>
<li role="option" tabindex="0" data-option-name="2016/17" data-option-id="54" data-option-index="2">
2016/17
</li>
</ul>
</div>
由于dropdownList类在HTML中被多次使用,并且我所需的元素位于第二个位置,即当您在这种情况下获得超出范围的索引时,这意味着找到的元素没有或只有一个,因为您编写的代码是正确的,我想您输入了错误的URL。 但若URL是正确的,那个么您可以使用XPATH找到合适的元素。 请尝试以下代码:
select_element = driver.find_element_by_xpath("//li[@data-option-name='2017/18']")
如果您能够使用python和selenium单击下拉列表。然后您可以尝试以下代码: 更新: 更新1: 说明: 显式等待是您定义的代码,用于在继续执行代码之前等待特定条件发生。最糟糕的情况是Thread.sleep,它将条件设置为等待的确切时间段。提供了一些方便的方法,可以帮助您编写只在需要时等待的代码。WebDriverWait与ExpectedCondition相结合是实现这一目标的一种方法
有关显式等待的更多信息,请参见是否可以单击下拉列表?我知道它不是用select和span标记制作的。我是说用python和selenium?事实上这就是我要做的。但是,如果您检查下面的答案,也会导致上面的错误,这也会生成我在下面发布的错误。我得到错误:raiseexception_classmessage,screen,stacktrace selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法找到元素:{method:xpath,selector://li[@data option name='2017/18']}这是不够的,但请尝试:li_tags=driver.find_elements_by_tag_nameli用于li_标记中的项目:if item.text='2017/18':item.click
select_element = driver.find_element_by_xpath("//li[@data-option-name='2017/18']")
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(executable_path = r'C:/Users/user***/Downloads/chromedriver_win32/chromedriver.exe')
driver.maximize_window()
wait = WebDriverWait(driver,40)
driver.get("https://www.premierleague.com/stats/top/players/goals")
wait.until(EC.visibility_of_element_located((By.ID, 'dd-FOOTBALL_COMPSEASON')))
time.sleep(5)
drop_down_click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.current[data-dropdown-current='FOOTBALL_COMPSEASON']")))
drop_down_click.click()
options = driver.find_elements_by_css_selector("ul[data-dropdown-list='FOOTBALL_COMPSEASON'] li")
for option in options:
if "2017/18" in option.text.strip():
option.click()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(executable_path = r'C:/Users/user***/Downloads/chromedriver_win32/chromedriver.exe')
driver.maximize_window()
wait = WebDriverWait(driver,40)
driver.get("https://www.premierleague.com/stats/top/players/total_scoring_att")
cookie_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.btn-primary.cookies-notice-accept")))
ActionChains(driver).move_to_element(cookie_button)
driver.execute_script('arguments[0].click();', cookie_button)
wait.until(EC.visibility_of_element_located((By.ID, 'dd-FOOTBALL_COMPSEASON')))
time.sleep(5)
drop_down_click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.current[data-dropdown-current='FOOTBALL_COMPSEASON']")))
drop_down_click.click()
options = driver.find_elements_by_css_selector("ul[data-dropdown-list='FOOTBALL_COMPSEASON'] li")
for option in options:
if "2017/18" in option.text.strip():
option.click()