从javascript函数webdriver访问动态生成的href

时间:2016-02-19 04:29:10

标签: javascript jquery python selenium selenium-webdriver

我想要访问一个棘手的href。我无法对此URL进行硬编码,因为它是由页面生成的,我将使用该程序好几年:

基础课程 https://gist.github.com/codyc4321/724f05aca8f6775e2fc1(感兴趣的access_link

class HCCDriver(FirefoxDriver):

    def __init__(self, username="cchilders", password="mypw"):
        super(HCCDriver, self).__init__(username=username, password=password, start_url="https://hccadvisor.hccfl.edu")
        self.username  = username
        self.password  = password

    def login_testout(self):
        driver = ChromeDriver()
        driver.get("http://www.testout.com/")
        login_a_tag = driver.access_link("hLogin")

我想要StartLogin()生成的内容:

<a id="hLogin" class="blue-button" href="javascript:StartLogin();" style="margin: 0 0 0 8px; border-right: 0; float: none; vertical-align: middle">Login</a>

错误:

In [3]: h.login_testout()
---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-3-3c353756ce70> in <module>()
----> 1 h.login_testout()

/home/cchilders/scripts/my_scripting_library/webdriver/specific.py in login_testout(self)
    119         driver = ChromeDriver()
    120         driver.get("http://www.testout.com/")
--> 121         login_a_tag = driver.access_link("hLogin")
    122 
    123     def access_class_search_screen_hcc(self, category="CTS", driver=None):

/home/cchilders/scripts/my_scripting_library/webdriver/general.pyc in access_link(self, search_text)
     89         element = self.locate_element(search_text)
     90         link  = element.get_attribute('href')
---> 91         self.get(link)
     92 
     93     def submit_form(self, search_text=None):

/home/cchilders/scripts/my_scripting_library/webdriver/general.pyc in get(self, url)
     64 
     65     def get(self, url=None):
---> 66         self.driver.get(url)
     67 
     68     @property

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.pyc in get(self, url)
    211         Loads a web page in the current browser session.
    212         """
--> 213         self.execute(Command.GET, {'url': url})
    214 
    215     @property

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.pyc in execute(self, driver_command, params)
    199         response = self.command_executor.execute(driver_command, params)
    200         if response:
--> 201             self.error_handler.check_response(response)
    202             response['value'] = self._unwrap_value(
    203                 response.get('value', None))

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.pyc in check_response(self, response)
    192         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    193             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 194         raise exception_class(message, screen, stacktrace)
    195 
    196     def _value_or_default(self, obj, key, default):

WebDriverException: Message: unknown error: unsupported protocol
  (Session info: chrome=48.0.2564.109)
  (Driver info: chromedriver=2.9.248304,platform=Linux 3.19.0-15-generic x86_64)

最糟糕的是,代码就像我使用FirefoxDriver一样,出于显而易见的原因,我更喜欢使用chrome作为学校作业。可以通过chrome访问此链接吗?谢谢

2 个答案:

答案 0 :(得分:1)

好的,我的描述非常混乱,但简而言之,如果您的网页上有f = csv.writer(open('/mydir/ntuser.csv', 'a'), dialect=csv.excel, delimiter='\t') f.writerow((first, second, rest)) 标记,并且您希望在javascript中获得href,请执行以下操作:

<a>

$("#hLogin").prop("href");

答案 1 :(得分:1)

您也可以点击该链接并获取.current_url

element = self.locate_element(search_text)
element.click()

link = self.driver.current_url