1

I want to use Python Selenium to automate something.

There is a menu that appears only when we passes the mouse on it without clicking (below "Main Menu") and in the list, we put the mouse on a item (below "Item 2") and a submenu appears. At this moment, we can click on item of the submenu (below "Subitem 2.2"). I want to automate the clicking on Subitem 2.2 for example.

Main menu (mouseover):

  • -Item 1
  • -Item 2 : -Subitem 2.1
      • -Subitem 2.2

Problem, when I click with the right to get the code source, nothing happens and I cannot get the exact ID of the items. So I have to look for it in the code source in Chrome (CTRL + U).

Below is the part of code source that interests me:

<<div id="type1_1n0Items" class="type1_1_0 type1_1_8">
    <table border="0" cellpadding="0" cellspacing="0">
        <tr onmouseover="Menu_HoverDynamic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(event)" id="type1_1n1">
            <td><table class="type1_1_7" cellpadding="0" cellspacing="0" border="0" width="100%">
                <tr>
                    <td style="white-space:nowrap;width:100%;"><a class="type1_1_1 type1_1_6" href="javascript:__doPostBack(XXXXXX;\\Item1&#39;)"><img src="../image/cog_edit.png" alt="" style="border-style:none;vertical-align:middle;" />Item 1</a></td>
                </tr>
            </table></td>
        </tr><tr onmouseover="Menu_HoverDynamic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(event)" id="type1_1n2">
            <td><table class="type1_1_7" cellpadding="0" cellspacing="0" border="0" width="100%">
                <tr>
                    <td style="white-space:nowrap;width:100%;"><a class="type1_1_1 type1_1_6" href="javascript:__doPostBack(XXXXXX;\\Item2&#39;)"><img src="../image/bullet_wrench.png" alt="" style="border-style:none;vertical-align:middle;" />Item 2</a></td>
                </tr>

My Python code:

time.sleep(5)

element_to_hover_over=browser.find_element_by_id("type1_1n0Items")
hover = ActionChains(browser).move_to_element(element_to_hover_over)
hover.perform()

Error I get:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="type1_1n0Items"]"}
  (Session info: chrome=89.0.4389.114)

Thanks for your help !

Meriole
  • 127
  • 8

2 Answers2

0

Are you sure the page is loaded ? If not you could do a loop to check if this element has appeared. link to explainations on the webdriverwait module :

https://selenium-python.readthedocs.io/waits.html

If it is not the problem : You can try to find the element with its class :

element_to_hover_over = browser.find_element_by_xpath("//div[@class='MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary']")

Nevertheless i am not sure of the synthax

Good luck ^^ Hope i helped

Tom Kuntz
  • 55
  • 1
  • 7
0

Thank you that you took time for it. I found the solution. I'm newbie in Python and forgot an important information that was the cause of the problem

The function clicks on an link that opens a second window. The element to locate is in the new window and I was convinced that Python automatically searchs for it in the new window, but it's not the case.

So I put use the window_handle and switch_to_window. Solution that I found here How to switch to new window in Selenium for Python?

It's now working very good.

window_before = browser.window_handles[0]
open_kalk=browser.find_element_by_id("XX")
open_kalk.click()
time.sleep(5)

window_after = browser.window_handles[1]
browser.switch_to_window(window_after)
        

element_to_hover_over=browser.find_element_by_id("type1_1n0Items")
hover = ActionChains(browser).move_to_element(element_to_hover_over)
hover.perform()
Meriole
  • 127
  • 8