Selenium - 将值插入下拉菜单(不存在 'Select' 标记)

Selenium - Inserting Values Into Dropdown Menus (Where No 'Select' Tag Exists)

我正试图从这里抓取每日梦幻棒球预测:https://www.numberfire.com/mlb/daily-fantasy/daily-baseball-projections/batters

我想浏览 'Platform' table 下拉选项,以使用 selenium 获取 DraftKings 的投影(FanDuel 设置为 table 默认值)。按照这个答案 Selenium (Python): How to insert value on a hidden input? 我正在尝试更改 class 'dfs-option-drop-value'.

的隐藏值
from selenium import webdriver

driver = webdriver.Chrome('/Applications/chromedriver')
URL = 'https://www.numberfire.com/mlb/daily-fantasy/daily-baseball-projections/batters'
driver.get(URL)

elem = driver.find_element_by_xpath(
    '//i[@class="nf-icon icon-caret-down active"]'
    '/following-sibling::input[@type="hidden"]')

value = driver.execute_script('return arguments[0].value;', elem)
print("Before update, hidden input value = {}".format(value))

driver.execute_script('''
    var elem = arguments[0];
    var value = arguments[1];
    elem.value = value;
''', elem, '4')

value = driver.execute_script('return arguments[0].value;', elem)
print("After update, hidden input value = {}".format(value))


# Then using beautiful soup
page = driver.page_source
soup = BeautifulSoup(page.content, 'html.parser')

dfs-option-drop-value 已根据需要更改,但页面仍呈现 FanDuel 投影,因此,显然这不是适当的 table 更改。请注意,这个特定的 table 没有带选项值的 'select' 标签,因此 Selenium dropdown menu and 之类的答案将不起作用。

有什么想法吗?

要打开该下拉菜单和 select 'FanDuel',您可以执行以下操作:

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

#This will open the drop-down dialog
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//span[@class='title' and(contains(text(),'Platform'))]/../div[@class='custom-drop']"))).click()

#This will select DraftKings from the open drop-down dialog.
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//ul[contains(@class,'active')]//li[@data-value='4']"))).click()