I am trying to create a prompt for a number from the user on a web page while using selenium in python.
This is the code I have written but it returns None
JavaScript
x
7
1
driver = webdriver.Chrome()
2
driver.get('https://www.google.com')
3
4
input_number = driver.execute_script('return parseInt(prompt("Enter a number", 20));')
5
6
print(input_number)
7
Advertisement
Answer
So I figured out the answer to my question.
Here is the code for anyone who might have the same issue:
JavaScript
1
15
15
1
from selenium.common.exceptions import UnexpectedAlertPresentException
2
3
driver = webdriver.Chrome()
4
driver.get('https://www.google.com')
5
6
while True:
7
try:
8
driver.execute_script("var a = prompt('Enter a number');document.body.setAttribute('user-manual-input', a)")
9
sleep(10) # must
10
print(driver.find_element_by_tag_name('body').get_attribute('user-manual-input')) # get the text
11
break
12
13
except (UnexpectedAlertPresentException):
14
pass
15