Selenium / Python:使用键盘输入

时间:2013-10-04 23:23:06

标签: python selenium

问题:无法选择元素,然后执行向下箭头的键盘输入。

情况:我正在打开一个页面,进行一些选择,并且在每次选择由.click()事件执行后,我想模拟向下箭头的键盘输入。我收到异常错误,并尝试了不同的方法,但没有成功。

错误:WebDriverException: Message: u'unknown error: cannot focus element\n (Session info: chrome=30.0.1599.69)\n (Driver info: chromedriver=2.3,platform=Mac OS X 10.8.4 x86_64)'

def test_00_validation_test(self):
        driver = self.driver
        driver.set_window_position(0,0)
        driver.maximize_window()
        driver.get(self.base_url)
        driver.find_element_by_id("open").click()
        driver.find_element_by_id("screen_name").send_keys("user")
        driver.find_element_by_id("password").send_keys("pwd")
        driver.find_element_by_id("login_submit").click()
        driver.find_element_by_id("card5").click()
        driver.find_element_by_id("make_picks").click()
        driver.find_element_by_id("team3").click()
        driver.find_element_by_id("team3").send_keys("\25")
        driver.find_element_by_id("team2").click()
        driver.find_element_by_id("team2").send_keys("\25")
        driver.find_element_by_id("submit_picks").click()
        self.assertEqual("Picks Saved", self.close_alert_and_get_its_text())

2 个答案:

答案 0 :(得分:0)

 switch_to_alert()
    Switches focus to an alert on the page.

doc

你必须专注于open_popup才能在弹出窗口中发送密钥

答案 1 :(得分:0)

找到了一个有效的解决方案使用driver.execute_script("window.scrollTo(0,0);")