1

The application I am testing searches for an account based on the email ID. The email ID is unique. The result gets displayed in the form of a table with various columns. One of which is "Account Name" displaying the name of the user/account holder linked to the email address in question. In order to go into the account, one has to click on this "Account Name" value which is a link. This "Account Name" link is dynamic based on the email ID we use every time. On inspecting this link I get this:

<a href="/001m000000pFY6U?srPos=0&amp;srKp=001" data-seclke="Account" data-seclkh="60761f49cf4ed8788252c560b733bef0" data- seclki="001m000000pFY6U" data-seclkp="/001m000000pFY6U" data-seclkr="1" onmousedown="searchResultClick.mousedown(this, event)" xpath="1" style="">F John</a>

I am wondering if there is a way to retrieve/extract only the href="/001m000000pFY6U?srPos=0&amp;srKp=001" bit from the above.

I tried the following:

elem = self.driver.find_elements_by_xpath("//a[@href]")
print(elem)

and this prints 120 lines something similar to the following: selenium.webdriver.remote.webelement.WebElement (session="502b43c903be36c997d4882f26f3c7ad",element="0.4252537892711272- 1")

Would appreciate a little help. Thanks.

1
  • Hi Dan can you share the full output of the href's as in my answer? Commented Jan 28, 2019 at 13:52

4 Answers 4

1

Try the following line of code:

account_links = [link.get_attribute("href") for link in driver.find_elements_by_css_selector("a[data-seclke='Account']")]

where account_links will be a list of your needed links.

Hope it helps you!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Ratmir Asanov. It worked and returned a list and the second element at index 1, is what I need. See this: ['cs20.salesforce.com/01m000000TpFY6U/…', 'cs20.salesforce.com/001m000000pTY6U?srPos=0&srKp=001', 'mailto:[email protected]']
0

You should use get_attribute.

For example:

elems = self.driver.find_elements_by_xpath("//a[@href]")
for i in elems:
     print(i.get_attribute("href")

Hope this helps you!

7 Comments

Thanks Moshe Slavin for your response. However I tried that as well but it prints 124 lines of some javascript code like: javascript:openPopupFocusEscapePounds(%27https://help.salesforce.com/apex/htdoor?loc=help&target=search_refine_a.htm&section=Search&language=en_US&release=218.6.2&instance=CS20&showSplash=true%27, %27Help%27, 1024, 768, %27width=1024,height=768,resizable=yes,toolbar=yes,status=yes,scrollbars=yes,menubar=yes,directories=no,location=yes,dependant=no%27, false, false); javascript:openPopupFocusEscapePounds(%27https://help.salesforce.com/apex/
Hi @DanSmith, can you share the full html anchor tag line of the above output?
<a href="/001m000000pFY6U?srPos=0&amp;srKp=001" data-seclke="Account" data-seclkh="60761f49cf4ed8788252c560b733bef0" data-seclki="001m000000pFY6U" data-seclkp="/001m000000pFY6U" data-seclkr="1" onmousedown="searchResultClick.mousedown(this, event)">F Chau</a> Thanks @Ali
Try this elements = driver.find_elements_by_xpath("//a") for i in elements: print i.get_attribute('href'), I have posted the answer
Hi @Moshe Slavin, XPath for finding the anchor tag 'a' I have changed
|
0

Try the below code :

elements = driver.find_elements_by_xpath("//a")
for i in elements:
    print i.get_attribute('href')

'a' will identify all the anchors then by using the attribute 'href', we can get the links.

1 Comment

No Ali CSE it does not.. Returns a lot of entries as mentioned earlier. Regards
0

Please try this code.Check if works

  shref=driver.find_element_by_xpath('//a[@data-seclke="Account"]').get_attribute("href")
    print(shref)

1 Comment

Thanks Kajal. It returns one entry now which I can use to navigate to the account. However that is not what I want I want to click on the Account name link in the table (as mentioned in my original post) and then go into the account.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.