【问题标题】:How to return async function Selenium Python如何返回异步函数 Selenium Python
【发布时间】:2021-03-21 08:03:55
【问题描述】:

嘿,这是我想要执行并返回值的异步 js 函数。

async function matcheslol() {
    let allmatches=[]
    let matches = document.getElementsByClassName('EventCellstyles__Link-sc-1m83enb-0 dhKVQJ')
    for (i = 0; i < matches.length; i++) {
        let details = (matches[i].innerText).split('\n').slice(2, 4);
        let matchname = details.join(' - ');
        console.log(matchname)
        matches[i].firstChild.click()
        await sleep(3000)
        let votes = document.getElementsByClassName('styles__VotingWrapper-sc-1f9zoyc-1 iOsjnp')[0].innerText.split('\n').slice(0,3)
        let date = document.getElementsByClassName('styles__Wrapper-sc-1c9nn5b-0 HoJZc ps ps--active-y')[1].getElementsByClassName('Content-sc-1o55eay-0 gYsVZh')[0].innerText
        let dict={'DATE':date,'MatchTitle':matchname,'Votes':votes}
        allmatches.push(dict)
    }
    return allmatches
}
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }
  
matcheslol()

Python:

data=driver.execute_async_script(script_mentioned_above)

我收到此错误: selenium.common.exceptions.TimeoutException: Message: script timeout

【问题讨论】:

    标签: javascript python selenium


    【解决方案1】:

    我的做法:

    # Create the script you want
    my_script = '''
    async function matcheslol() {
      // Your previous function here...
    }
    
    // Set it to window so we can use it later
    window.matcheslol = matcheslol;
    '''
    
    # Set window variable
    driver.execute_script(my_script)
    # Execute the function and return the value
    value = driver.execute_script('return window.matcheslol().then(r => r);')
    

    【讨论】:

      猜你喜欢
      • 2020-07-29
      • 2023-03-08
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      相关资源
      最近更新 更多