单击 JS link 并弹出

Clicking on JS link with splash

我正在调查主题,因为有一个站点在单击所述 link 后可以访问部分数据。

我有这样的脚本:

#!/usr/bin/env python

script="""
splash:go(splash.args.url)
splash:wait(10)

splash:runjs("$('a[data-bet-type=FixedPlace]')[0].click()")
splash:wait(10)

return {
  -- html = splash:html(),
  png = splash:png(),
  -- har = splash:har(),
}
"""

from requests import post
from json import dumps, dump
from base64 import b64decode
from contextlib import suppress

if 1:
    endpoint='http://localhost:8050/run'
    j={
        'lua_source': script,
        'url': 'https://www.odds.com.au/horse-racing/bunbury/race-1',
        'timeout': 90,
        'headers': {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0'
        }
    }

r=post(endpoint, headers={'Content-Type': 'application/json'}, data=dumps(j))
print(r.status_code)
if r.status_code != 200:
    print(r.text)
    exit()
j=r.json()

for _ in ('html', 'har'):          
    with suppress(KeyError), open('1.'+_, 'w') as f: f.write(j[_])
with suppress(KeyError), open('1.png', 'wb') as f: f.write(b64decode(j['png']))

if 'har' in j:
    for entry in j['har']['log']['entries']:
        url=entry['request']['url']
        if url.startswith('https://www.odds.com.au'): print(url)

但是,虽然它完美地呈现了页面,但点击并没有发生。我试过 ya.ru 并且相同的方法有效(但在那种情况下它是一个按钮)。我没主意了。尝试设置 UA,玩等待(它不快,那个页面),使用 js_source,但无法点击任何东西,虽然我看到元素已找到。

您使用 CSS 选择器 定位的 a 元素不可点击 - 查看其 href 值如何设置为 link 一无所获:

<a href="javascript:void(-1);" data-event-id="665605" data-bet-type="FixedPlace">place</a>

您需要点击父 li 元素:

$('.bettype__switcher li:last-child')[0].click()

对我有用 - 选择了 "Place" 投注类型。