【问题标题】:Change Value of Element within Python with JS and Selenium使用 JS 和 Selenium 在 Python 中更改元素的值
【发布时间】:2021-06-04 21:23:23
【问题描述】:

好的,首先我不想惹恼这里的任何人我对编码还很陌生,而且我很清楚在这些论坛上提问应该是你最后去的地方,而且只有在你用谷歌搜索了一段时间之后。反正我有问题。我尝试将 2captcha 用于我的 python 网络抓取自动化。当我运行脚本时,我从 2captcha 和他们的 API 文档中获取验证码令牌,他们说,我必须将令牌放入带有 fc-token 的元素的值中(屏幕截图)

现在我想把token放到html代码的value=""中然后提交。

这是我迄今为止尝试过的:

    ## Gets the captcha Token
time.sleep(15)
url=str(web.current_url)
solver = TwoCaptcha(APIKEY)

try:
    result = solver.funcaptcha(sitekey='B7D8911C-5CC8-A9A3-35B0-554ACEE604DA',
                               url=url,
                               surl='https://client-api.arkoselabs.com')
except Exception as e:
    sys.exit(e)

print(result['code'])
## The Captcha Token
token= result['code']

time.sleep(10)

##Switching to the right Iframe 
web.switch_to.frame('enforcementFrame')

##Here Im trying to change the value of the Element "FunCaptcha-Token" with JS
web.execute_script('''
  let {token} = arguments
  document.querySelector('FunCaptcha-Token').value = token
''', token)

现在,当我运行脚本时,我得到了令牌,所以 2captcha 部分应该可以工作,但 Python 中的 JS 部分不起作用。这就是我得到的错误代码:

KeyError: 'document.getElementById("FunCaptcha-Token").value=87260ba9ceb681253.3489941501|r=us-ea [...] 继续使用令牌

现在我将添加 HTML 代码。

<input type="hidden" id="FunCaptcha-Token" name="fc-token" value="91560ba8aabd2a839.9381249805|r=eu-west-1|metabgclr=%23ffffff|maintxtclr=%231B1B1B|mainbgclr=%23ffffff|guitextcolor=%23747474|metaiconclr=%23757575|meta=7|lang=de|pk=B7D8911C-5CC8-A9A3-35B0-554ACEE604DA|at=40|ht=1|atp=2|cdn_url=https%3A%2F%2Fclient-api.arkoselabs.com%2Fcdn%2Ffc|lurl=https%3A%2F%2Faudio-eu-west-1.arkoselabs.com|surl=https%3A%2F%2Fclient-api.arkoselabs.com">

对不起,我的长篇文章我提前谢谢你。

【问题讨论】:

    标签: javascript python selenium


    【解决方案1】:

    应该是这样的:

    driver.execute_script('''
      let {token} = arguments
      document.querySelector('#FunCaptcha-Token').value = token
    ''', token)
    

    然后你想提交表单或者运行回调

    【讨论】:

    • 嘿,非常感谢您的回答不幸的是我无法让它工作我收到此错误消息:selenium.common.exceptions.JavascriptException:消息:javascript 错误:无法设置属性“值”为空
    • 这意味着它不存在。也许它在 iframe 中?
    • 嘿,感谢您的快速响应我实现了代码我仍然收到相同的错误消息。
    • 我们需要查看网址,抱歉。
    • 好吧,在outlook.live.com的注册过程中,这相当困难
    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 2021-12-11
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2017-01-18
    • 2020-02-12
    • 1970-01-01
    相关资源
    最近更新 更多