Selenium将值插入下拉菜单(不存在“选择”标记)

2024-09-27 07:33:56 发布

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

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

我想通过“平台”表格下拉选项导航,以使用selenium获取绘图投影(FanDuel设置为表格默认值)。根据这个答案Selenium (Python): How to insert value on a hidden input?我正在尝试更改类“dfs选项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选项的drop值会根据需要进行更改,但页面仍呈现扇形投影,因此,显然这不是适当的表更改。请注意,这个特定的表没有带有选项值的“select”标记,因此Selenium dropdown menupython - scraping tables by navigating different options in drop down list之类的答案将不起作用

有什么想法吗


Tags: httpsinputexecutevaluewww选项driverpage
1条回答
网友
1楼 · 发布于 2024-09-27 07:33:56

要打开该下拉列表并选择“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()

相关问题 更多 >

    热门问题