简体   繁体   English

无法使用 Selenium Python 定位元素

[英]Unable to locate element with Selenium Python

I am trying to click on an option from a menu using selenium with Python and I can't seem to get it to work.我试图从使用 selenium 和 Python 的菜单中单击一个选项,但我似乎无法让它工作。 Below is the HTML code.下面是 HTML 代码。 I have tried Action Chains with move_to_element(menu).click(submenu) where menu is the boardMenu-content and submenu is the boardMenu-reports... and that didn't work.我已经尝试过使用move_to_element(menu).click(submenu)操作链,其中menuboardMenu-contentsubmenuboardMenu-reports...但没有奏效。

<div class="boardMenu" tabindex="0">
  <div class="boardMenu-content">
    <button class="boardMenu-reports boardMenu-item boardMenu-itemButton">
      <i class="icons-reports boardMenu-icon"></i>
      Reports
    </button>
  

I tried chaining the actions together like:我尝试将这些操作链接在一起,例如:

board = driver.find_element_by_class_name("boardMenu")
menu = driver.find_element_by_class_name("boardMenu-content")
rep = driver.find_element_by_class_name("boardMenu-reports boardMenu-item boardMenu-itemButton")
actions = ActionChains(driver)
actions.move_to_element(board)
actions.move_to_element(menu)
actions.click(rep)
actions.perform()

but when I tried that I get the error that it can't find the element for rep .但是当我尝试这样做时,我收到错误,它找不到rep的元素。 I also don't understand though why it is running rep in its line as apposed to in actions.click(rep) I have also deleted the chain action section and just run them one at a time with time.sleep(3) to give it all time to load and whatnot.我也不明白为什么它在它的行中运行repactions.click(rep)我也删除了链操作部分,并且只用time.sleep(3)一次运行它们一个它总是加载什么的。 Still get the error that it is unable to locate element.仍然得到无法定位元素的错误。

So to solve this I learned something new about Inspect Element.所以为了解决这个问题,我学到了一些关于 Inspect Element 的新知识。 I would right click on the section I want then hit copy and choose xpath and use that with Selenium.我会右键单击我想要的部分,然后点击复制并选择 xpath 并将其与 Selenium 一起使用。 Maybe that is a well known technique to use but it was certainly new to me.也许这是一种众所周知的技术,但对我来说肯定是新的。

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

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