[我正在使用Python和Selenium,但在尝试向下滚动直到selenium找到经典的“显示更多”按钮(似乎是隐藏的)时,我不得不抓取页面。
直到现在我所做的是:
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys element = browser.find_element_by_xpath('//*[@id="ember371"]/button') # you can use ANY way to locate element coordinates = element.location_once_scrolled_into_view # returns dict of X, Y coordinates browser.execute_script('window.scrollTo({}, {});'.format(coordinates['x'], coordinates['y']))
但是,无论我做什么Python都会抛出几种错误,例如以下。
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ember371"]/button"} (Session info: chrome=80.0.3987.122)
是否可能是因为按钮被隐藏?
下面是我要查找的按钮的HTML代码,我想单击该按钮。
<button class="pv-profile-section__card-action-bar pv-skills-section__additional-skills artdeco-container-card-action-bar artdeco-button artdeco-button--tertiary artdeco-button--3 artdeco-button--fluid" aria-controls="skill-categories-expanded" aria-expanded="false" data-control-name="skill_details" data-ember-action="" data-ember-action-2182="2182"> <span aria-hidden="true"> Show more </span> <span class="visually-hidden"> BLA BLA BLA </span> <li-icon aria-hidden="true" type="chevron-down-icon" class="pv-skills-section__chevron-icon" size="small"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" data-supported-dps="16x16" fill="currentColor" width="16" height="16" focusable="false"> <path d="M8 9l5.93-4L15 6.54l-6.15 4.2a1.5 1.5 0 01-1.69 0L1 6.54 2.07 5z"></path> </svg></li-icon> </button>
您是否愿意帮助我?
我正在使用Python和Selenium,但在尝试向下滚动直到selenium找到经典的“显示更多”按钮(似乎是隐藏的)时,我被卡住了。我所做的直到现在...
您可以在此处尝试使用while循环,检查是否存在按钮作为条件,并将browser.execute_script放入正文中。这样,您将首先检查按钮是否存在,如果不存在,则滚动200 px左右(请确保不要滚动经过按钮)直到按钮出现。
您首先可以尝试使用isEnabled()方法来查找是否已启用“显示更多”按钮。如果在网页上启用了指定的Web元素,则此方法返回“ true”值;否则,如果在网页上禁用了Web元素,则返回“ false”值。
所需的元素是EmberJS启用的元素,因此对于scrollIntoView
,您需要为element_to_be_clickable()
引入WebDriverWait