Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the second element from DOM using selenium and python [duplicate]

I have a 2 elements on my web page that has the same class name and I am trying to access the second element and I am unable to do that.

The span looks like this:

<span class="REPORTING_DASHBOARDS__link navMenuLabel ">

Dashboards

</span>

My code for this part looks like this:

dashboards_button = driver.find_element_by_css_selector(".REPORTING_DASHBOARDS__link.navMenuLabel")[1]                                                          
dashboards_button.click()
like image 544
PyRar Avatar asked Oct 19 '18 14:10

PyRar


1 Answers

You have to use find_elements_ instead of find_element_ to get all element. find_element returns only first possible element.

dashboards_button = driver.find_elements_by_css_selector(".REPORTING_DASHBOARDS__link.navMenuLabel")[1] 

Or using nth-child if it is under same parent with find_element

dashboards_button = driver.find_element_by_css_selector(".REPORTING_DASHBOARDS__link.navMenuLabel:nth-child(1)")   

if it is not under same parent, change it to xpath,

dashboards_button = driver.find_element_by_xpath("//[contains(@class,'REPORTING_DASHBOARDS__link navMenuLabel')][1]")   
like image 129
Navarasu Avatar answered Oct 18 '22 20:10

Navarasu