![](/img/trans.png)
[英]How do I click on the 'Load More' button from the Tradingview website using Selenium and python?
[英]How do I click on a button on a website using selenium - 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>
這里有一個加載更多按鈕,我想使用 selenium 單擊它。
為此,您應該使用selenium
模塊中的click()
function。 我們將通過CSS selector
找到按鈕 - 這將減少點擊錯誤元素的機會
self.webdriver.find_element_by_css_selector('input[class="amscroll-load-button"]').click()
但是,我建議使用該代碼
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(self.webdriver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[class="amscroll-load-button"]'))).click()
此代碼檢查按鈕是否可點擊 X 秒,如果是,它將被點擊。
我能夠通過使用以下代碼來解決這個問題:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.