0

I have a tricky href I want to access. I cannot hardcode this URL because it gets generated by the page, and I will use this program for several years:

Base classes https://gist.github.com/codyc4321/724f05aca8f6775e2fc1 (access_link of interest)

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")

I want what StartLogin() generates:

<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>

Error:

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)

Worst part is, the code works as if I use FirefoxDriver, I prefer chrome for schoolwork for obvious reasons. Can this link be accessed in chrome? Thank you

codyc4321
  • 9,014
  • 22
  • 92
  • 165

2 Answers2

1

Ok that description was extremely confusing to me, but in short, if you have an <a> tag on your page and you want to get the href in javascript, just do:

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

or

$("#hLogin").attr("href");
wpercy
  • 9,636
  • 4
  • 33
  • 45
Macainian
  • 362
  • 2
  • 10
  • 1
    You can include html elements in your answers by enclosing them in ticks: ` – wpercy Feb 22 '16 at 17:49
  • Ahh, thanks. I thought the only way to put code in was to use four spaces. While we are on the topic, what is the best way to put code in an answer? Should I write it somewhere else and copy-paste it in, or is there an easier way to write it in here on stackoverflow? Typing all those spaces is really tedious. – Macainian Feb 22 '16 at 20:27
  • 1
    If it's too long to do the four spaces before every line I'll write it in sublime, select all and hit tab - it indents it four spaces and then you can select it all and paste it into an answer. – wpercy Feb 22 '16 at 20:57
  • Ah yeah, genius. I forget about editors and their ability to fake tabs by creating spacing in their place. Thanks. – Macainian Feb 22 '16 at 21:02
1

You can also click the link and get the .current_url:

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

link = self.driver.current_url
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I have an odd issue, this site pops open a completely new window, which isn't an instance of the driver. Trying to grab the `href` attrib and put that into a new driver fails...may need open a new question – codyc4321 Feb 19 '16 at 04:39
  • ya I included the URL in the func. it's a mess – codyc4321 Feb 19 '16 at 04:39
  • @codyc4321 you might need to switch to that new window using `driver.switch_to.window(handle)`..http://stackoverflow.com/questions/13113954/selenium-webdriver-using-switch-to-windows-and-printing-the-title-doesnt-prin. – alecxe Feb 19 '16 at 04:41