相同的链接在循环中打开

时间:2015-06-18 08:58:37

标签: python selenium-webdriver

我是Python新手,我正在尝试编写一个用于测试网站链接的小代码。我创建了一个字典如下:

header_dropdown_links = {
            'Features' : 'Features', 
            'Company' : 'Company | Values',
            'Community' : 'Community | Open Sauce',
            'Solutions' : 'Selenium Testing | Solutions',
            'Resources' : 'Resources',
            'Enterprise' : 'Enterprise',
            'Sign Up' : 'Sign Up',
            'Docs' : 'Docs',
            'Pricing' : 'Pricing',
            'Login' : 'Login'      
       }

左侧是链接名称,右侧是页面标题。

我创建了以下表达式,以便在循环中找到每个链接并将其与标题匹配。 对于header_dropdown_links中的链接:

header_link = '//nav[@id ="global"]//a[contains(text(),' + link + ')]'
        driver.find_element_by_xpath(header_link).click()

现在,当我运行此代码时,我每次只点击“资源”链接,但它匹配所有标题我不清楚为什么?有人可以帮帮我吗?

-------------------------被修改--------------------- -----------

我正在尝试检查字形中的链接(3行图标)。 我写的代码:

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import unittest

header_dropdown_links = {
        'Features' : 'Features', 
        'Company' : 'Company | Values',
        'Community' : 'Community | Open Sauce',
        'Solutions' : 'Selenium Testing | Solutions',
        'Resources' : 'Resources',
        'Enterprise' : 'Enterprise',
        'Sign Up' : 'Sign Up',
        'Docs' : 'Docs',
        'Pricing' : 'Pricing',
        'Login' : 'Login'      
    }

class SauceLabsHeader(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = 'https://saucelabs.com/'
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_sauce_labs_header(self):
        driver = self.driver

        for link in header_dropdown_links: 

            #opens SauceLabs Home Page
            driver.get(self.base_url)

            #clicks on Glyph for opening the drop down menu
            driver.find_element_by_class_name('hamburger').click()

            #finding link by xpath
            header_link = '//nav[@id ="global"]//a[contains(text(),' + link + ')]'
            driver.find_element_by_xpath(header_link).click()

            #verifying if the title matches
            expected_title = header_dropdown_links[link]
            try: self.assertRegexpMatches(driver.title,expected_title)
            except AssertionError as e: self.verificationErrors.append(str(e))


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

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

我得到的错误:

   EE
======================================================================
ERROR: test_sauce_labs_header (__main__.SauceLabsHeader)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\-kuhu-\git\CS82A_Automation\src\SauceLabs_Glyph_WebDriver.py", line 42, in test_sauce_labs_header
    driver.find_element_by_xpath(header_link).click()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 232, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 664, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 175, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
InvalidSelectorException: Message: The given selector //nav[@id ="global"]//a[contains(text(),Sign Up)] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression //nav[@id ="global"]//a[contains(text(),Sign Up)] because of the following error:
SyntaxError: The expression is not a legal expression.
Stacktrace:
    at FirefoxDriver.annotateInvalidSelectorError_ (file:///c:/users/-kuhu-/appdata/local/temp/tmpjbvptb/extensions/fxdriver@googlecode.com/components/driver-component.js:10245)
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/-kuhu-/appdata/local/temp/tmpjbvptb/extensions/fxdriver@googlecode.com/components/driver-component.js:10276)
    at FirefoxDriver.prototype.findElement (file:///c:/users/-kuhu-/appdata/local/temp/tmpjbvptb/extensions/fxdriver@googlecode.com/components/driver-component.js:10280)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/-kuhu-/appdata/local/temp/tmpjbvptb/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/-kuhu-/appdata/local/temp/tmpjbvptb/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
    at DelayedCommand.prototype.execute/< (file:///c:/users/-kuhu-/appdata/local/temp/tmpjbvptb/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)

======================================================================
ERROR: test_sauce_labs_header (__main__.SauceLabsHeader)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\-kuhu-\git\CS82A_Automation\src\SauceLabs_Glyph_WebDriver.py", line 52, in tearDown
    self.assertEqual([], self.verificationErrors)
AssertionError: Lists differ: [] != ["Regexp didn't match: 'Featur...

Second list contains 2 additional elements.
First extra element 0:
Regexp didn't match: 'Features' not found in u'Resources'

- []
+ ["Regexp didn't match: 'Features' not found in u'Resources'",
+  "Regexp didn't match: 'Enterprise' not found in u'Resources'"]

----------------------------------------------------------------------
Ran 1 test in 52.697s

FAILED (errors=2)

控制台上的header_link和expected_title的打印值:

//nav[@id ="global"]//a[contains(text(),Resources)]
Resources
//nav[@id ="global"]//a[contains(text(),Features)]
Features
//nav[@id ="global"]//a[contains(text(),Enterprise)]
Enterprise
//nav[@id ="global"]//a[contains(text(),Sign Up)]
Sign Up
//nav[@id ="global"]//a[contains(text(),Docs)]
Docs
//nav[@id ="global"]//a[contains(text(),Company)]
Company | Values
//nav[@id ="global"]//a[contains(text(),Pricing)]
Pricing
//nav[@id ="global"]//a[contains(text(),Login)]
Login
//nav[@id ="global"]//a[contains(text(),Community)]
Community | Open Sauce
//nav[@id ="global"]//a[contains(text(),Solutions)]
Selenium Testing | Solutions

2 个答案:

答案 0 :(得分:0)

你确定这部分是正确的吗?

header_link = '//nav[@id ="global"]//a[contains(text(),link)]'

不应该 -

header_link = '//nav[@id ="global"]//a[contains(text(),"' + link + '")]'

您希望link作为包含在字符串中的字符串,还是希望每个链接中都有文字,例如Resources

答案 1 :(得分:0)

仍显示错误:

# on each of your nodes, start the swarm agent
#  <node_ip> doesn't have to be public (eg. 192.168.0.X),
#  as long as the swarm manager can access it.
$ swarm join --addr=<node_ip:2375> token://<cluster_id>