Find element using Python + Selenium

Asked

Viewed 1,090 times

0

Hello, I am trying to click on an element using webdriver with pythone Selenium

The element :

<a class="ppm_button_menu_item" alt="Exportar para CSV" href="javascript:clarity.uitk.exportToFile( 'npt.gridCsvExport&amp;RhXm0r7tSeUqEr=true&amp;odf_view=projectCreate.subObjList.cop_prj_statusrpt&amp;odf_code=project&amp;parentObjectCode=project&amp;id=5180135&amp;odf_concrete_parent_object_code=project&amp;object=cop_prj_statusrpt&amp;view_code=cop_prj_statusrptList&amp;componentId=odf&amp;portlet_id=projmgr.projectProperties&amp;portlet_instance_id=cop_prj_statusrptList&amp;portlet_instance_type=SYSTEM&amp;page_id=projmgr.projectProperties', '25000', 'cop_prj_statusrptList')" onclick="event.cancelBubble=true;"> button1 </a>

The x.path of the element:

//*[@id="d560c4a2-e016-41f9-b56b-697b8fb4f5e6"]/div[1]/div[1]/span[3]/span[2]/a[3]
The problem is; This id number is auto generated and everytime the page is reload its change, so I cant use in the code.

The element that the ID of the previous code refers to is the following:

<form name="page" method="POST" id="d560c4a2-e016-41f9-b56b-697b8fb4f5e6" autocomplete="off" type="form" action="nu" onsubmit="return false;"><input type="hidden" name="scenarioId" value=""><div class="ppm_filter_section" aria-expanded="false">

I tried to make python find the button like this:

campo = driver.find_element_by_xpath(
        '//*[@name="page"]/div[1]/div[1]/span[3]/span[2]/a[3]')
campo.click()

I don’t know what else to do :(

  • there are other tags formor just this one?

1 answer

1


Hello, first I would like to know if the page in question is public, if it is and want to share we could better inspect what is happening.

But I will leave a solution that I have already used a lot, a javascript feature that you can run via Selenium.

from the presumed knowledge in basic javascript, open the browser on the page where the tag is and open the developer console of google Chrome and do a search by tag as per:

document.getElementsByTagName('a')[0]

if on the page there is more than one tag of type "a", I suggest you change the initial value from zero to: "('a')[1]", "('a')[2]", "('a')[3]" until you find the position of the element you want (if you do this by the google console Chrome it will highlight which object you are referring to).

after finding the position of the element among the others of the same tag just add a . click() at the end:

document.getElementsByTagName('a')[0].click()

Pressing enter should happen one-click action, and if it works it might include it in your code like this:

driver.execute_script('document.getElementsByTagName('a')[0].click()')

without access to a test page this is the only solution I can offer, I ask you to test and report the result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.