如何使用硒-python单击网站上的按钮?

时间:2020-07-12 16:39:54

标签: python selenium

我正在尝试使用硒在页面上单击“加载更多”。我使用了以下css_selector代码:

element= driver.find_element_by_css_selector('#amscroll-page-2]')
driver.execute_script("arguments[0].scrollIntoView(true);", element)

我遇到以下错误。

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".amscroll-load-button"}
  (Session info: chrome=83.0.4103.61)

我使用了xpath代码:

elemnent=driver.find_element_by_xpath('//*[@id="amscroll-page-2"]')
driver.execute_script("arguments[0].scrollIntoView();", element)
time.sleep(5)
login=element.click()

我遇到以下错误

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="amscroll-page loading" id="amscroll-page-2" rel="2">...</div> is not clickable at point (382, 18). Other element would receive the click: <div class="naaod__content">...</div>
  (Session info: chrome=83.0.4103.61)

直到我要向下滚动并单击的位置的html代码是:

    <div class="amscroll-page loading" id="amscroll-page-2" rel="2"><input type="button" 
class="amscroll-load-button" style="background: #2675C2;" onclick="amscroll_object.loadNextPage(2);" value="Load More"></div>

这里有一个加载更多按钮,我想使用硒单击。

2 个答案:

答案 0 :(得分:2)

为此,您应该使用onSave() { var timeSplit = this.registerConfigForm.value.end.split(':'), hours, minutes, meridian; hours = timeSplit[0]; minutes = timeSplit[1]; if (hours > 12) { meridian = 'PM'; hours -= 12; } else if (hours < 12) { meridian = 'AM'; if (hours == 0) { hours = 12; } } else { meridian = 'PM'; } alert(hours + ':' + minutes + ' ' + meridian); } 模块中的click()函数。我们将通过selenium找到该按钮-这将减少点击错误元素的机会

CSS selector

但是,我建议使用该代码

self.webdriver.find_element_by_css_selector('input[class="amscroll-load-button"]').click()

此代码检查按钮是否可以在X秒钟内单击,如果单击,它将被单击。

答案 1 :(得分:1)

我能够使用以下代码解决此问题:

button = driver.find_element_by_xpath("/html/body/div[3]/div/div[1]/div[2]/div[2]/div[2]/div[3]/div[1]")
ActionChains(driver).move_to_element(button).click(button).perform()
相关问题