21

我正在使用 Selenium WebDriver 并在 Python 中编码

我到处看了看,我能找到的最好的是用不同语言写的东西。我也尝试在 Selenium IDE 上使用导出工具,但是当我查看数据时说不支持导出该功能。

编辑:我需要以更大的分辨率打开浏览器的原因是因为我正在测试的 Web 应用程序支持平板电脑分辨率,因此元素会根据浏览器窗口的分辨率而有所不同。

这是我从 IDE 导出的脚本,并进行了一些修改。


from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import Select

from selenium.common.exceptions import NoSuchElementException

import unittest, time, re

from Funk_Lib import RS


class CreatingEditingDeletingVault(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://cimdev-qa40/"
        self.verificationErrors = []


    def test_creating_editing_deleting_vault(self):
        driver = self.driver
        driver.get(self.base_url + "/Login?contoller=Home")
        driver.find_element_by_id("UserName").click()
        driver.find_element_by_id("UserName").clear()
        driver.find_element_by_id("UserName").send_keys("user@gocodigo.com")
        driver.find_element_by_name("Password").click()
        driver.find_element_by_name("Password").clear()
        driver.find_element_by_name("Password").send_keys("Codigo#123")
        driver.find_element_by_id("fat-btn").click()
        driver.get(self.base_url + "/Content/Vaults/")
        driver.find_element_by_link_text("Content").click()
        driver.find_element_by_link_text("Vaults").click()
        driver.find_element_by_css_selector("button.btn.dropdown-toggle").click()
        driver.find_element_by_link_text("New vault").click()
        driver.find_element_by_name("Name").clear()
        driver.find_element_by_name("Name").send_keys("Test Vault")
        driver.find_element_by_xpath("//button[@onclick=\"vault_action('createvault', null, $('#CreateVault [name=\\'Name\\']').val())\"]").click()
        driver.find_element_by_css_selector("button.btn.dropdown-toggle").click()
        driver.find_element_by_link_text("Rename vault").click()
        driver.find_element_by_name("Id").click()
        Select(driver.find_element_by_name("Id")).select_by_visible_text("Test Vault")
        driver.find_element_by_css_selector("option[value=\"2\"]").click()
        driver.find_element_by_name("Name").clear()
        driver.find_element_by_name("Name").send_keys("Test Change")
        driver.find_element_by_xpath("//button[@onclick=\"vault_action('renamevault', $('#RenameVault [name=\\'Id\\']').val(), $('#RenameVault [name=\\'Name\\']').val())\"]").click()
        driver.find_element_by_css_selector("button.btn.dropdown-toggle").click()
        driver.find_element_by_link_text("Delete vault").click()
        driver.find_element_by_name("Id").click()
        Select(driver.find_element_by_name("Id")).select_by_visible_text("Test Change")
        driver.find_element_by_css_selector("option[value=\"2\"]").click()
        driver.find_element_by_xpath("//button[@onclick=\"vault_action('deletevault', $('#DeleteVault [name=\\'Id\\']').val(), '')\"]").click()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
4

4 回答 4

37

硒 2.31.0

driver = webdriver.Firefox()

# Resize the window to the screen width/height
driver.set_window_size(300, 500)

# Move the window to position x/y
driver.set_window_position(200, 200)
于 2013-03-14T18:19:55.667 回答
26
browser = webdriver.Firefox()
url = 'http://www.google.com/'
browser.get(url)
browser.maximize_window()
于 2013-04-17T05:04:20.267 回答
5

您可以使用selenium.windowMaxmize();,或者如果您想以某种特定的分辨率运行测试,您可以使用

selenium.getEval("window.resizeTo(X, Y); window.moveTo(0,0);")

于 2012-11-27T05:02:20.197 回答
3

您是否要求增加屏幕放大倍率?

这将放大 Firefox 浏览器 Python 2.7

from selenium.webdriver.common.keys import Keys    

br = webdriver.Firefox()
zoomAction = ActionChains(br)
body = br.find_element_by_tag_name('body')
for i in range(2):
    zoomAction.send_keys_to_element(body,Keys.CONTROL,"+").perform()
于 2014-10-28T19:10:46.127 回答