English 中文(简体)
我如何利用Selenium/Webdriver来推动投入,并利用结果?
原标题:How can I prompt for input using Selenium/Webdriver and use the result?

我谨允许用户提出意见,并根据这些意见作出一些决定。 如果我这样做的话:

driver.execute_script("prompt( Enter smth , smth )")

我听到了一点,但我不能利用它的价值。 是否有任何办法向用户显示输入箱,并使用该箱的数值?

EDIT:这是我的文字:

from selenium.webdriver import Firefox

if __name__ == "__main__":
    driver = Firefox()
    driver.execute_script("window.promptResponse=prompt( Enter smth , smth )")
    a = driver.execute_script("var win = this.browserbot.getUserWindow(); return win.promptResponse")
    print "got back %s" % a

除以下例外:

    a = driver.execute_script("var win = this.browserbot.getUserWindow(); return win.promptResponse")
  File "c:python26libsite-packagesselenium-2.12.1-py2.6.eggseleniumwebdriver
emotewebdriver.py", line 385, in ex
ecute_script
    { script : script,  args :converted_args})[ value ]
  File "c:python26libsite-packagesselenium-2.12.1-py2.6.eggseleniumwebdriver
emotewebdriver.py", line 153, in ex
ecute
    self.error_handler.check_response(response)
  File "c:python26libsite-packagesselenium-2.12.1-py2.6.eggseleniumwebdriver
emoteerrorhandler.py", line 110, in
 check_response
    if  message  in value:
TypeError: argument of type  NoneType  is not iterable

我没有做什么?

EDIT:我试图像预告那样做,这里的产出是:

In [1]: from selenium.webdriver import Firefox

In [2]: f = Firefox()

In [3]: a = f.ex
f.execute              f.execute_async_script f.execute_script

In [3]: a = f.execute_script("return prompt( Enter smth , smth )")

In [4]: a
Out[4]: {u text : u Enter smth }

In [5]: a
Out[5]: {u text : u Enter smth }

In [6]: class(a)
  File "<ipython-input-6-2d2ff4f61612>", line 1
    class(a)
         ^
SyntaxError: invalid syntax


In [7]: type(a)
Out[7]: dict
问题回答

希望有助于其他人:

# selenium (3.4.1)  python (3.5.1)
driver.execute_script("var a = prompt( Enter Luffy ,  Luffy );document.body.setAttribute( data-id , a)")
time.sleep(3)  # must 
print(self.driver.find_element_by_tag_name( body ).get_attribute( data-id ))   # get the text

You are correct in using the prompt box in javascript. But the prompt box value should be assigned to a global variable and then you could use this variable later. something like this:

driver.execute_script(”window.promptResponse=prompt() 加入:

and then retrieve the value from the same global variable.

a = driver.execute_script("var win = this.browserbot.getUserWindow(); return win.promptResponse")

你们也许需要放弃回报。

Hope this helps.

我知道这是一个老的问题,但我也存在同样的问题,这对我来说是什么事:有些信贷到@Devin。

from selenium.common.exceptions import UnexpectedAlertPresentException

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

     except (UnexpectedAlertPresentException):
        pass

很快将等待5秒的投入。 如果没有投入,将促使用户再次提供投入。

Tkinter是一个基于全球倡议的图书馆,你可以在操作时间从用户那里获取信息。 除非用户输入信息,否则该方案将持续等待。 关于预先设计的对话箱,请参阅link。 虽然它很晚,但它可能帮助别人。

根据其他答复,我制定了以下法典,为我工作:


def is_alert_present(driver):
    try:
        driver.switch_to.alert
        return True
    except exceptions.NoAlertPresentException:
        return False


def prompt_user(driver, text):

    driver.execute_script( var a = prompt ("{}");document.body.setAttribute("data-id", a) .format(text))
    while is_alert_present(driver):
        sleep(4)
    v = driver.find_element_by_tag_name( body ).get_attribute( data-id )

    return v if v !=  null  else None

为什么不能直接收回价值?

if __name__ == "__main__":
    driver = Firefox()
    a = driver.execute_script("return prompt( Enter smth , smth )")
    print "got back %s" % a

我在C#中的工程。 诚然,它含有略为老的硫化物,但我 would期望<代码>execute_script功能大为改变。

如果你用ium2.28像我这样说的话,那么就象@Baz1nga这样trick。

//Open the prompt inbox and setup global variable to contain the result
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.promptResponse = prompt("Please enter captcha");");

//Handle javascript prompt box and get value. 
Alert alert = driver.switchTo().alert();
try {
  Thread.sleep(6000);
} catch (Exception e)
{
  System.out.println("Cannot sleep because of headache");
}
alert.accept();
String ret = (String) js.executeScript("return window.promptResponse;");




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签