【问题标题】:How to write a test to verify using Selenium Web Browser Automation如何编写测试以使用 Selenium Web 浏览器自动化进行验证
【发布时间】:2016-05-28 03:30:36
【问题描述】:

我对 Selenium Web 浏览器自动化非常陌生。 我正在尝试编写一个测试来验证关于的菜单项实际上进入了“http://www.seleniumhq.org/”中的关于页面 你们知道如何使用 Selenium Web 浏览器自动化 Python 来实现这一目标吗? 谢谢你!

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    通过链接文本找到About链接,点击它并检查driver.title的值是什么:

    from selenium import webdriver
    
    
    driver = webdriver.Firefox()
    driver.get("http://www.seleniumhq.org/")
    
    driver.find_element_by_link_text("About").click()
    
    assert driver.title == "About Selenium"
    
    driver.close()
    

    【讨论】:

      【解决方案2】:

      首先,确定加载的关于页面中的内容(元素、文本、链接、img 等),您将使用它来验证您是否在正确的(关于)页面中。 然后像这样构造你的代码-

      from selenium import webdriver
      from selenium.webdriver.common.keys import Keys
      
      driver = webdriver.Chrome()
      
      driver.implicitly_wait(10)
      driver.set_page_load_timeout(10)
      driver.get("http://www.seleniumhq.org")
      driver.find_element_by_id("menu_about").click()
      #Assertion 1
      assert 'About' in driver.title
      #Assertion 2
      assert 'about' in driver.current_url
      #console log
      print('Title of the current page is: ' + driver.title)
      print('Current URL is : ' + driver.current_url)
      driver.close()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多