我正在研究使用硒进行网络爬网,但发现了一些错误。
这是我的编码如下。
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 != "billing"" 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 != "billing"" 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 === "divider", disabled: submenu.disabled }" class="ng-scope"><!-- ngIf: submenu.disabled --><!-- ngIf: submenu !== "divider" && !submenu.disabled --><a ng-if="submenu !== "divider" && !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 !== "divider" && !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>
答案 0 :(得分:0)
Selenium具有一些功能来等待元素加载。 了解有关selenium.webdriver.support.expected_conditions的信息。
默认情况下,预期的条件会等待30秒左右,以使元素显示在页面上。如果您在这些网站上四处点击,则非常有用。
documentation有一个很好的例子。