did12345
did12345

Reputation: 57

How to click on a javascript element using SELENIUM automation and python?

I am trying to develop a script that will tell you when there are available rendez vous at a certain service.

For this i am trying to use SELENIUM automation to interact with the page to check the available rendez vous slots (when there are any)

The problem i am having is that the web page contains javascript!

full page: https://pastel.diplomatie.gouv.fr/rdvinternet/html-4.02.00/frameset/frameset.html?lcid=1&sgid=193&suid=2

here is the basic code of the page:

<html><head>
    <title>Menu</title>
    <link rel="stylesheet" type="text/css" href="/rdvinternet/FrameworkERGO-2.12.1/css/menu.css">
    <link rel="stylesheet" type="text/css" href="/rdvinternet/FrameworkERGO-2.12.1/css/commun/globale.css">
    <link rel="stylesheet" type="text/css" href="/rdvinternet/FrameworkERGO-2.12.1/css/commun/cg_commun.css">
    <link rel="stylesheet" type="text/css" href="/rdvinternet/FrameworkERGO-2.12.1/css/couleur/cg_codeCouleur.css">
</head>
<body marginwidth="0" marginheight="0">
<div id="divMenu" style="">
<table valign="top" cellpadding="2" cellspacing="0" border="0" width="100%" height="100%">
<tbody>
<tr id="zoneDebutMenu" style="display:none;"><td><div id="DebutMenu"></div></td></tr>
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tbody><tr>
<td class="menu1Item1" onclick="javascript:parent.parent.ComposantMenuFrameset.SelectItem1Menu1(0,false)" onmouseover="javascript:parent.parent.ComposantMenuFrameset.Item1SetUnSelectedOverClass(0)" onmouseout="javascript:parent.parent.ComposantMenuFrameset.Item1OnMouseOut(0)" width="168">&nbsp;&nbsp;<span id="item1_0" class="menuSelected11">Rendez-vous</span></td>
<td class="FlecheMenu2Item1" width="16" align="absmiddle"><img src="/rdvinternet/FrameworkERGO-2.12.1/images/bouton/divers/spacer.gif" alt="" height="16" width="16&quot;" border="0"></td>

</tr>
<tr>
<td colspan="2"><div id="subItems1_0" style="">
<table cellpadding="0" cellspacing="0" border="0">
<tbody><tr>
<td class="menu1Item2" onclick="javascript:parent.parent.ComposantMenuFrameset.SelectItem2Menu1(0,0,false)" onmouseover="javascript:parent.parent.ComposantMenuFrameset.Item2SetUnSelectedOverClass(0,0)" onmouseout="javascript:parent.parent.ComposantMenuFrameset.Item2OnMouseOut(0,0)"><div id="item2_0_0" class="menuUnselected12"> Prendre rendez-vous</div></td>
</tr>
<tr>
<td class="menu1Item2" onclick="javascript:parent.parent.ComposantMenuFrameset.SelectItem2Menu1(0,1,false)" onmouseover="javascript:parent.parent.ComposantMenuFrameset.Item2SetUnSelectedOverClass(0,1)" onmouseout="javascript:parent.parent.ComposantMenuFrameset.Item2OnMouseOut(0,1)"><div id="item2_0_1" class="menuUnselected12"> Revoir son rendez-vous</div></td>
</tr>
</tbody></table>
</div>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
</tbody></table>
</td>
</tr>
<tr id="zoneFinMenu" style="display:none;"><td valign="bottom"><div id="FinMenu"></div></td></tr>
<tr><td valign="bottom"><div id="piedDePage" class="piedDePage">© Ministère de l'Europe et des Affaires étrangères</div></td></tr>
</tbody>
</table>
</div>

</body></html>

I am trying to click on an element using:

find_element_by_class_name('menu1Item2').click()

and:

find_element_by_id('item2_0_0').click()

and neither methods have worked to click on the element :( any ideas?

Upvotes: 0

Views: 77

Answers (1)

Fabio Pereira
Fabio Pereira

Reputation: 11

I've looked the html from the top level to the element, and the element is inside two nested <iframe> tags (which is odd). In order to interact with elements inside iframes, you must change the browser context via the driver.switch_to.frame() method.

This is the code

    driver.get('https://pastel.diplomatie.gouv.fr/rdvinternet/html-4.02.00/frameset/frameset.html?lcid=1&sgid=193&suid=2')
    
    sleep(3) # Wait the iframes content to load.
    driver.switch_to.frame('BODY_WIN')
    driver.switch_to.frame('MENU_WIN')
    
    driver.find_elements(By.ID, 'item2_0_0').click()

You can improve this code by changing the sleep() for one of the selenium wait methods.

References:

Switch to iframe

Selenium documentation

Selenium Waits

Upvotes: 1

Related Questions