跟随Selenium IDE到Python - 没有这样的元素



这是我第一次体验Selenium和HTML。

我尝试使用IDE,然后按照它指示的"方式"。 我的 IDE 录制脚本说

Command | Target
-----------------------------------------------
click   | css=#hamburger-btn
click   | css=div.menu-item.menu-item-selected > span.menu-item-title
click   | css=div.menu-item.menu-item-selected > div.menu-panel-content > div.menu-category-cont > div.menu-category-sub > div.menu-category-sub-item.menu-category-sub-item-selected > span.menu-category-sub-item-title

我试图以这种方式实现它:

browser = webdriver.Chrome()
browser.get(url) #url was defined before
browser.find_element_by_css_selector("#hamburger-btn").click()
browser.find_element_by_css_selector("div.menu-item.menu-item-selected>span.menu-item-title").click()
browser.find_element_by_css_selector('div.menu-item.menu-item-selected>div.menu-panel-content>div.menu-category-cont>div.menu-category-sub>div.menu-category-sub-item.menu-category-sub-item-selected>span.menu-category-sub-item-title').click()

但我收到此"没有此类元素"消息/错误

browser.find_element_by_css_selector('div.menu-item.menu-item-selected>div.menu-panel-content>div.menu-category-cont>div.menu-category-sub>div.menu-category-sub-item.menu-category-sub-item-selected>span.menu-category-sub-item-title').click()
File "C:Python27libsite-packagesseleniumwebdriverremotewebdriver.py", line 597, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "C:Python27libsite-packagesseleniumwebdriverremotewebdriver.py", line 966, in find_element
'value': value})['value']
File "C:Python27libsite-packagesseleniumwebdriverremotewebdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:Python27libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.menu-item.menu-item-selected>div.menu-panel-content>div.menu-category-cont>div.menu-category-sub>div.menu-category-sub-item.menu-category-sub-item-selected>span.menu-category-sub-item-title"}

我做错了什么?

谢谢

国标

也许您尝试访问的元素未正确加载。 尝试添加一个等待语句,

类似于
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.common.by import By
...
browser = webdriver.Chrome()
browser.get(url) #url was defined before
browser.find_element_by_css_selector("#hamburger-btn").click()
browser.find_element_by_css_selector("div.menu-item.menu-item-selected>span.menu-item-title").click()
WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'div.menu-item.menu-item-selected>div.menu-panel-content>div.menu-category-cont>div.menu-category-sub>div.menu-category-sub-item.menu-category-sub-item-selected>span.menu-category-sub-item-title')))
browser.find_element_by_css_selector('div.menu-item.menu-item-selected>div.menu-panel-content>div.menu-category-cont>div.menu-category-sub>div.menu-category-sub-item.menu-category-sub-item-selected>span.menu-category-sub-item-title').click()

这是考虑到这个元素实际上存在于你的页面中,并且你用你的css选择器正确地解决了它。

相关内容

  • 没有找到相关文章

最新更新