gpt4 book ai didi

javascript - 如何在 Python 中使用 Selenium 从 JS 提示符中获取 Int

转载 作者:行者123 更新时间:2023-12-02 22:37:51 25 4
gpt4 key购买 nike

我正在尝试在 python 中使用 selenium 时在网页上创建用户输入号码的提示。

这是我编写的代码,但它返回None

driver = webdriver.Chrome()
driver.get('https://www.google.com')

input_number = driver.execute_script('return parseInt(prompt("Enter a number", 20));')

print(input_number)

最佳答案

所以我找到了问题的答案。

以下代码可供可能遇到相同问题的任何人使用:

from selenium.common.exceptions import UnexpectedAlertPresentException

driver = webdriver.Chrome()
driver.get('https://www.google.com')

while True:
try:
driver.execute_script("var a = prompt('Enter a number');document.body.setAttribute('user-manual-input', a)")
sleep(10) # must
print(driver.find_element_by_tag_name('body').get_attribute('user-manual-input')) # get the text
break

except (UnexpectedAlertPresentException):
pass

关于javascript - 如何在 Python 中使用 Selenium 从 JS 提示符中获取 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58684468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com