简体   繁体   English

Python Selenium - 无法定位元素,问题出在哪里?

[英]Python Selenium - Unable to locate element, where is the problem?

I want to use Python Selenium to automate something.我想用 Python Selenium 来自动化一些东西。

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.只有当我们将鼠标移过它而不单击时才会出现一个菜单(在“主菜单”下方),并且在列表中,我们将鼠标放在一个项目上(在“项目 2”下方)并出现一个子菜单。 At this moment, we can click on item of the submenu (below "Subitem 2.2").此时,我们可以点击子菜单项(在“子项2.2”下方)。 I want to automate the clicking on Subitem 2.2 for example.例如,我想自动单击 Subitem 2.2。

Main menu (mouseover):主菜单(鼠标悬停):

  • -Item 1 -项目 1
  • -Item 2: -Subitem 2.1 - 项目 2: - 子项目 2.1
      • -Subitem 2.2 - 子项目 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.问题,当我点击获取代码源的权利时,什么也没有发生,我无法获得项目的确切 ID。 So I have to look for it in the code source in Chrome (CTRL + U).所以我必须在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:我的 Python 代码:

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 !谢谢你的帮助 !

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:链接到 webdriverwait 模块的解释:

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

If it is not the problem: You can try to find the element with its class:如果不是问题:您可以尝试使用其 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祝你好运^^希望我有所帮助

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我是 Python 的新手,忘记了导致问题的重要信息

The function clicks on an link that opens a second window. function 点击打开第二个 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.要定位的元素在新的 window 中,我确信 Python 会自动在新的 window 中搜索它,但事实并非如此。

So I put use the window_handle and switch_to_window.所以我使用了window_handle 和switch_to_window。 Solution that I found here How to switch to new window in Selenium for Python?我在这里找到的解决方案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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM