Maria Santos
Maria Santos

Reputation: 9

Python Selenium Locating Element and Clicking on it

I have been stuck in this line of code for over 2 weeks now, and I am hoping someone might come to my assistance!

I am using python selenium to navigate through a website which contains a database. I want to click on a link which will open a new tab, from which I am going to extract the information that I am looking for. I only need to do this once, so no need for any sort of loops! However, I have tried multiple times and I have been getting the ElementClickInterceptedException error message every time. I have tried using WebDriverWait, and then I get NoSuchElement error message. I've tried searching the element through several ways, including id, class name, xpath, and link text, but no luck!

The HTLM code looks like this:

<a id="reportsForm:j_idt290:4:j_idt295" href="#" class="ui-commandlink ui-widget nav-item " onclick="PrimeFaces.addSubmitParam('reportsForm',{'reportsForm:j_idt290:4:j_idt295':'reportsForm:j_idt290:4:j_idt295'}).submit('reportsForm','_blank');return false;" target="_blank"><span id="reportsForm:j_idt290:4:l2" data-hasqtip="reportsForm:j_idt290:4:tooltipID" aria-describedby="qtip-reportsForm:j_idt290:4:tooltipID">Análise Financeira e de Gestão</span></a>
  <span id="reportsForm:j_idt290:4:l2" data-hasqtip="reportsForm:j_idt290:4:tooltipID" aria-describedby="qtip-reportsForm:j_idt290:4:tooltipID">Análise Financeira e de Gestão</span>

So far, I have tried:

afg = driver.find_element_by_link_text("Análise Financeira e de Gestão") 

afg = driver.find_element_by_xpath("//*[contains(text(), 'Análise Financeira e de Gestão')]")

WebDriverWait(driver,10).until(expected_conditions.element_to_be_clickable((By.ID,"reportsForm:j_idt290:4:j_idt295"))).click()

afg = driver.find_element_by_id("j_idt290:4:j_idt295") 

I think the problem might be because the link text is not immediately after the href, and not even in the same line! And so, when I search by link text, it can't be found...

Also, sometimes, without changing the code at all, just by running it again, with the driver.find_element_by_id method, it works out of nowhere once or twice, but then its stops working again!

Any ideas on how to solve this problem?

Thank you for your help!!! :)

Upvotes: 1

Views: 130

Answers (2)

ErickChi95
ErickChi95

Reputation: 1

Maybe the element you're trying to hit is found within a frame, if that's the case you should switch to that iframe first and then you'll be able to perform the desired action on that element

Upvotes: 0

JD308
JD308

Reputation: 146

You can try this xpath.

//a[normalize-space(.)="Análise Financeira e de Gestão"]

Upvotes: 0

Related Questions