【问题标题】:Extracting JavaScript function return value using Python Selenium使用 Python Selenium 提取 JavaScript 函数返回值
【发布时间】:2021-01-19 19:14:25
【问题描述】:

我正在从网页中抓取信息。以下是我希望从中提取返回字符串值的 Javascript 函数的 sn-p。在这种情况下"2227885"

我使用 selenium 中的以下方法来尝试提取此值:

result = driver.execute_script("getCurrentClientId()[0]")
print(result)

但是,返回的值是None。从这个 JS 函数中提取返回值的正确解决方案是什么?

【问题讨论】:

    标签: javascript python selenium selenium-webdriver


    【解决方案1】:

    在你的 javascript 中使用“return”:

    result = driver.execute_script("return getCurrentClientId()[0]")
    print(result)
    

    【讨论】:

      【解决方案2】:
      result = driver.execute_script("return getCurrentClientId()")
      print(result)
      

      如果函数不在 html 源代码中,并且您想显式传递,则使用

      result = driver.execute_script("return arguments[0]", getCurrentClientId)
      print(result)
      

      我们传递给 execute_script 的任何东西都会以相同的顺序传递到参数数组中。因此 getCurrentClientId 可以在脚本中作为 arguments[0]

      访问

      【讨论】:

        猜你喜欢
        • 2014-04-01
        • 2016-03-27
        • 1970-01-01
        • 1970-01-01
        • 2017-04-05
        • 1970-01-01
        • 2016-04-25
        • 2018-09-22
        • 2016-01-07
        相关资源
        最近更新 更多