1

I'm trying to use Pythons Selenium module to click on an element whose link has the text "xlsx" at the end of it. Below is the code I'm using and the details of the element. Can someone please see why Python is unable to find this element?

driver.find_element_by_partial_link_text('xlsx').click()

Here is the element details:

<a name="URL$2" id="URL$2" ptlinktgt="pt_new" tabindex="43" onfocus="doFocus_win0(this,false,true);" href="http:******/HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx" onclick="window.open('http:********/HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx','','');cancelBubble(event);return false;" class="PSHYPERLINK">HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx</a>

I had to remove some parts of the URL for confidentiality purposes, however, it should not impact the answering of the question.

Thanks.

5
  • 3
    Python is unable to find this element.. Did you get NoSuchElementException? Check whether link located inside an iframe Commented Apr 8, 2018 at 19:47
  • This is what it says: no such element: Unable to locate element. Also, i'm not sure how to check if its a link inside an iframe, but i have a strong feeling that it is. Commented Apr 8, 2018 at 21:05
  • Hey, thanks to your question/comment, i found out it wasn't able to find the element because i had to switch to frame. So it worked when i added the following before the code i used: driver.switch_to.frame('ptModFrame_0') Commented Apr 8, 2018 at 21:30
  • How is going? Did you try my answer? Commented Apr 10, 2018 at 10:44
  • Hi, Yes i did and it didnt work. Turns out there is a frame involved so i had to use "swith_to.frame first. Commented Apr 10, 2018 at 22:40

4 Answers 4

1

Thanks for the replies. Turns out, as @Andersson mentioned, the window was in a different frame.

I solved the problem using the following code before the find_element: driver.switch_to.frame('ptModFrame_0').

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

Comments

1

You can use a CSS selector:

driver.find_element_by_css_selector("a[href*='xlsx']")

If the element still cannot be located, I would suggest using a wait statement, to ensure that the element is visible, before you interact with it.

1 Comment

Great answer Larry, I would also add this (XPath) works in a similar manner that the CSS selector does .find_element_by_xpath("//*[contains(text(),'ABC')]")
0

Please try: driver.find_element_by_xpath(".//a[contains(@href,'xlsx')]").

Comments

0

You can grab it by class name (class name = PSHYPERLINK).

This should work:

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

url = ''
driver = webdriver.Chrome('/path/to/chromedriver/executable')

if __name__=='__main__':
     driver.get(url)
     time.sleep(3)
     driver.find_element_by_class_name('PSHYPERLINK').click()

When finding the attribute, make sure to use a singular '"element". Like:

driver.find_element_by_class_name('PSHYPERLINK').click()

not:

driver.find_elements_by_class_name('PSHYPERLINK').click()

Hope this helps.

Comments

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.