猿问

Python Selenium_find_elements:列表索引超出范围错误

我想从Play商店获得一些评论。但是,当我向下滚动页面时,我需要按下“更多评论”按钮才能获得更多。我使用了 find_elements_by_class_name 和 click(),但它弹出列表索引超出范围错误。因此,我试图打印出来并显示“[]”。我不知道解决它。


# open all reviews 

# urls = ["https://play.google.com/store/apps/details? 

          id=com.fourdesire.fortunecity"]    


url = url+'&showAllReviews=true'

driver.get(url)

time.sleep(5)  # wait dom ready 

for i in range(1, 10):

    # scroll to load other reviews

    print(i)

    driver.execute_script(

        'window.scrollTo(0, document.body.scrollHeight);') 

    time.sleep(1)

page = driver.page_source  

soup_expatistan = BeautifulSoup(page, "html.parser")

element2 = driver.find_elements_by_class_name("RveJvd snByac")[0].click()


慕田峪9158850
浏览 234回答 2
2回答

慕虎7371278

import timefrom selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.common.exceptions import TimeoutExceptionclass FindByXpathCss():&nbsp; &nbsp; &nbsp; &nbsp; driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")&nbsp; &nbsp; &nbsp; &nbsp; driver.maximize_window()&nbsp; &nbsp; &nbsp; &nbsp; baseUrl = "https://play.google.com/store/apps/details?id=com.delta.mobile.android&hl=en_US&showAllReviews=true"&nbsp; &nbsp; &nbsp; &nbsp; driver.get(baseUrl)&nbsp; &nbsp; &nbsp; &nbsp; scrolls = 3&nbsp; &nbsp; &nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrolls -= 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if scrolls < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; buttonClick = WebDriverWait(driver, 30).until(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EC.visibility_of_all_elements_located((By.XPATH, "//button[contains(@class,'')][contains(text(),'Full Review')]")))&nbsp; &nbsp; &nbsp; &nbsp; for element in buttonClick:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; driver.execute_script("arguments[0].click();", element)&nbsp; &nbsp; &nbsp; &nbsp; reviewText = WebDriverWait(driver, 30).until(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EC.presence_of_all_elements_located((By.XPATH, "//*[@class='UD7Dzf']")))&nbsp; &nbsp; &nbsp; &nbsp; for textreview in reviewText:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print textreview.text&nbsp; &nbsp; &nbsp; &nbsp; reviewText = WebDriverWait(driver, 30).until(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EC.presence_of_all_elements_located((By.XPATH, "//*[@class='UD7Dzf']")))&nbsp; &nbsp; &nbsp; &nbsp; for textreview in reviewText:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print textreview.text第一个例子第二个例子

拉风的咖菲猫

如下更新最后一行。element2&nbsp;=&nbsp;driver.find_elements_by_class_name("RveJvd.snByac")[0].click()那是因为find_elements_by_class_name它将被 selenium 转换为 css.并将被添加到此处提供的定位器中。由于您的值有多个类,因此您必须将空格替换为.或者,您可以使用以下内容。driver.find_element_by_xpath("//*[@class='RveJvd&nbsp;snByac']").click()你可以使用find_element_by相当find_elements。
随时随地看视频慕课网APP

相关分类

Python
我要回答