[英]How to select a dropdown menu using Selenium Python
I'm working on a personal project, trying to use selenium to web scrape my locals teams results from a website.我正在做一个个人项目,尝试使用 selenium 从网站上抓取我当地团队的结果。 The site has three drop-down lists, at the moment I'd be happy to just be able to alter one.该站点有三个下拉列表,目前我很高兴能够更改一个。 The code below clicks reject cookies when I enter the page, that's what the WebDriver line does.当我进入页面时,下面的代码会单击拒绝 cookie,这就是 WebDriver 行的作用。 I'm getting as far as 'dropdown' and then an error that no such element exists.我得到了“下拉”,然后出现了一个错误,即不存在这样的元素。 I've tried CSS_SELECTOR, XPATH and all the other options to no avail.我试过 CSS_SELECTOR、XPATH 和所有其他选项都无济于事。 I am only new to this so it could be something simple I'm missing but I've read a lot of the previous forums with similar questions and the answers didn't work for me.我只是对此很陌生,所以这可能是我想念的一些简单的东西,但我已经阅读了很多以前的论坛都有类似的问题,但答案对我不起作用。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Firefox()
driver.get("https://www.leinsterrugby.ie/domestic-rugby/domestic-fixtures-results/")
time.sleep(3)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='onetrust-reject-all-handler']"))).click()
time.sleep(3)
dropdown=Select(driver.find_element(By.XPATH,value='//*[@id="groups_data"]')
time.sleep(5)
dropdown.select_by_value('5756')
driver.quit() # close browser
The element with dropdown options as Men
, Schools
, Women
, Youth
is within an <iframe>
so you have to:具有Men
、 Schools
、 Women
、 Youth
等下拉选项的元素位于<iframe>
中,因此您必须:
Induce WebDriverWait for the desired frame to be available and switch to it .诱导WebDriverWait使所需的帧可用并切换到它。
Induce WebDriverWait for the desired element to be clickable .诱导WebDriverWait使所需元素成为可点击的。
You can use either of the following Locator Strategies :您可以使用以下任一定位器策略:
Using CSS_SELECTOR :使用CSS_SELECTOR :
driver.get('https://www.leinsterrugby.ie/domestic-rugby/domestic-fixtures-results/') WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='https://shared2.sportsmanager.ie/~leinsterrugby/']"))) Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[name=user]")))).select_by_value('7183')
Using XPATH :使用XPATH :
driver.get('https://www.t-online.de/themen/e-mail') WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://shared2.sportsmanager.ie/~leinsterrugby/']"))) Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@name='user']")))).select_by_value('7183')
Note : You have to add the following imports :注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:浏览器快照:
Many of those lines can be simplified by using SeleniumBase .许多这些行可以通过使用SeleniumBase来简化。
Here's code that searches for results on that site and then prints the results: ( self.switch_to_frame()
is needed to switch to the iframe, and self.select_option_by_text()
is for the dropdowns.)这是在该站点上搜索结果然后打印结果的代码:( self.switch_to_frame()
需要切换到 iframe,而self.select_option_by_text()
用于下拉菜单。)
from seleniumbase import BaseCase
class RugbyResultsTests(BaseCase):
def test_get_rubgy_results(self):
self.open("https://www.leinsterrugby.ie/domestic-rugby/domestic-fixtures-results/")
self.switch_to_frame('iframe[src*="sportsmanager.ie/~leinsterrugby/"]')
self.sleep(1)
self.select_option_by_text("select#usercompyear", "2020-2021")
self.sleep(3)
self.select_option_by_text("select#groups_data", "Leinster Seconds League (J2)")
self.sleep(1)
self.click('a[href*="sportsmanager.ie/~leinsterrugby/league/155189"]')
self.sleep(2)
print("\n" + self.get_text("table.table-condensed"))
Output: (After running the above test with pytest
with seleniumbase
installed via pip
.)输出:(在通过pip
安装了seleniumbase
的pytest
运行上述测试之后。)
POS TEAM PLD W D L PF PA DIFF BP BP L DED PTS
1 Ashbourne 1 1 0 0 18 17 1 0 0 0 4
2 Gorey 1 1 0 0 0 0 0 0 0 0 4
3 Kilkenny 1 0 0 1 17 18 -1 1 1 0 1
4 Suttonians 0 0 0 0 0 0 0 0 0 0 0
5 Monkstown 0 0 0 0 0 0 0 0 0 0 0
6 Seapoint 0 0 0 0 0 0 0 0 0 0 0
7 Bective Rangers 0 0 0 0 0 0 0 0 0 0 0
8 Dundalk 1 0 0 1 0 0 0 0 0 0 0
The default browser for SeleniumBase tests is Chrome, but you can change that by calling pytest --firefox
. SeleniumBase测试的默认浏览器是 Chrome,但您可以通过调用pytest --firefox
来更改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.