【问题标题】:python selenium crawling Unable to locate element errorpython selenium爬取无法定位元素错误
【发布时间】:2019-01-25 22:31:35
【问题描述】:

我正在研究使用 selenium 进行网络爬虫,但发现了一些错误。

这是我的编码如下。

from selenium import webdriver as wd
main_url = 'https://searchad.naver.com/'
driver = wd.Chrome(executable_path='chromedriver.exe')

我登录了

# access main url2

main_url2 = 'https://searchad.naver.com/my-screen'

# close popup window
driver.find_element_by_css_selector('body > my-app > wrap > welcome-beginner-layer-popup > div.ly_main_pop > div.pop_top > a').click() 

# main3 page access
driver.find_element_by_css_selector('.btn_area>li').click()

# click tool menu bar
driver.find_element_by_css_selector('#navbar-common-header-collapse > ul > li:nth-child(3) > a > span.ng-binding.ng-scope').click()

从最后一次编码(#点击工具菜单栏),有错误为 错误信息:

消息:没有这样的元素:无法找到元素。 Css 选择器和 xpath 都是错误的。

这是我在'도구'上点击鼠标之前的资源代码如下。

<a class="dropdown-toggle ng-scope" ng-if="menu.items" href="" data-toggle="dropdown" role="button" aria-expanded="false"><!-- ngIf: menu.gnb != "billing" --><span ng-if="menu.gnb != &quot;billing&quot;" ng-bind-html="menu.translationId | translate" class="ng-binding ng-scope">도구</span><!-- end ngIf: menu.gnb != "billing" --><!-- ngIf: menu.gnb === "billing" --><span class="ico-dropdown-arrow"></span></a>

<!-- ngIf: menu.gnb != "billing" -->

<span ng-if="menu.gnb != &quot;billing&quot;" ng-bind-html="menu.translationId | translate" class="ng-binding ng-scope">도구</span> 

点击“도구”菜单后,可以找到“키워드 도구”的子菜单。这也是'키워드도구'的代码。

<li ng-repeat="submenu in menu.items track by $index" ng-class="{ divider: submenu === &quot;divider&quot;, disabled: submenu.disabled }" class="ng-scope"><!-- ngIf: submenu.disabled --><!-- ngIf: submenu !== "divider" && !submenu.disabled --><a ng-if="submenu !== &quot;divider&quot; &amp;&amp; !submenu.disabled" data-click-logger="" data-click-id="click.tool.keyword-planner" href="/customers/668860/tool/keyword-planner" ui-sref="tool.keyword-planner(vm.getParams({customerId: customerId}, submenu.params))" ui-sref-opts="{reload: submenu.reload}" ng-bind-html="submenu.translationId | translate" class="ng-binding ng-scope">키워드 도구</a><!-- end ngIf: submenu !== "divider" && !submenu.disabled --></li>
<!-- ngIf: submenu.disabled -->
<!-- ngIf: submenu !== "divider" && !submenu.disabled -->
<a ng-if="submenu !== &quot;divider&quot; &amp;&amp; !submenu.disabled" data-click-logger="" data-click-id="click.tool.keyword-planner" href="/customers/668860/tool/keyword-planner" ui-sref="tool.keyword-planner(vm.getParams({customerId: customerId}, submenu.params))" ui-sref-opts="{reload: submenu.reload}" ng-bind-html="submenu.translationId | translate" class="ng-binding ng-scope">키워드 도구</a>

enter image description here

【问题讨论】:

标签: javascript python selenium css-selectors web-crawler


【解决方案1】:

Selenium 有几个函数可以等待元素加载。 阅读 selenium.webdriver.support.expected_conditions。

默认情况下,预期条件会等待大约 30 秒,以便元素显示在页面上。如果您在网站上四处点击,这非常有用。

documentation 有一个很好的例子。

【讨论】:

  • 我做了 time.sleep(30) 但结果有同样的错误。感谢您的评论。
  • 你把睡眠放在哪里了?另外,请确保您 hover 菜单选项卡而不是 clicking 它。
  • 感谢您的评论。我不知道hover和Actionchain,所以我需要研究那个领域。
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
相关资源
最近更新 更多