[英]accessing dynamically generated href from a javascript function, webdriver
I have a tricky href I want to access. 我想访问一个棘手的href。 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) 基类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")
I want what StartLogin()
generates: 我想要
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>
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. 最糟糕的是,该代码就像使用FirefoxDriver一样工作,出于明显的原因,我更喜欢使用chrome做作业。 Can this link be accessed in chrome?
可以在Chrome中访问此链接吗? Thank you
谢谢
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: 好的,该描述对我来说非常混乱,但是简而言之,如果您的页面上有一个
<a>
标记,并且您希望在javascript中获取href,请执行以下操作:
$("#hLogin").prop("href");
or 要么
$("#hLogin").attr("href");
You can also click the link and get the .current_url
: 您还可以单击链接并获取
.current_url
:
element = self.locate_element(search_text)
element.click()
link = self.driver.current_url
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.