3 回答

TA貢獻1859條經(jīng)驗 獲得超6個贊
我會簡單地使用下面的CSS。
button[data-stid='show-more-results']
腳本:
# scroll to the element before clicking
driver.find_element_by_css_selector("button[data-stid='show-more-results']").location_once_scrolled_into_view
# click on the show more button
driver.find_element_by_css_selector("button[data-stid='show-more-results']").click()
截屏:

TA貢獻1780條經(jīng)驗 獲得超4個贊
試試: driver.findElement(By.xpath("//span[contains(text(),'Show More')]")).click()

TA貢獻1862條經(jīng)驗 獲得超7個贊
在視口中滾動后,您需要多次單擊帶有文本的按鈕Show More并實現(xiàn)此目的,您可以使用以下定位器策略:
代碼塊:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.expedia.com/Hotel-Search?adults=1&destination=Montreal%2C%20Quebec%2C%20Canada&endDate=2019-09-16&latLong=45.50195%2C-73.56714&localDateFormat=M%2Fd%2Fyyyy®ionId=178288&sort=recommended&startDate=2019-09-15&useRewards=true")
while True:
try:
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Show More']"))))
driver.execute_script("arguments[0].click();", WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Show More']"))))
print("Show More button clicked")
except:
print("No more Show More button")
break
driver.quit()
控制臺輸出:
Show More button clicked
Show More button clicked
Show More button clicked
.
No more Show More button
添加回答
舉報