在模式框架 Selenium Python 中多次单击 'Read more' 按钮
Clicking more than one time of 'Read more' button in modal frame Selenium Python
我想获取客人的所有评论。但是有些评论的文字很长,需要单击 'read more' 按钮才能查看所有文字。问题是按钮的数量取决于评论。我可以加载并获得所有评论,它工作正常,但我不知道 'read more' 按钮。如何在模式
中单击所有 'read more' 按钮
enter image description here
Link URL: https://th.airbnb.com/rooms/27194960/reviews?source_impression_id=p3_1600195106_a%2FYGw9bddHf%2BMfUE
下面的代码是从 URL 获取 HTML 文本的函数。有 2 个条件,我们将重点关注条件 2,它会获得您可以在 if-else 条件下看到的评论 --- if review:.
def get_pageswithSelenium(roomid,review,page_send):
#session = requests.Session()
#ua = UserAgent()
#headers = {'User-Agent':ua.random}
if not(review):
url = "https://th.airbnb.com/rooms/{}?source_impression_id=p3_1600195106_a%2FYGw9bddHf%2BMfUE".format(roomid)
else:
url = "https://th.airbnb.com/rooms/{}/reviews?source_impression_id=p3_1600195106_a%2FYGw9bddHf%2BMfUE".format(roomid)
print("selenium url: "+url)
browser = webdriver.Chrome(executable_path=r"C:\chromedriver_win32\chromedriver.exe")
browser.get(url)
if review:
browser.implicitly_wait(20)
element_inside_popup = browser.find_element_by_xpath('//div[@class="_yzu7qn"]//a')
for j in range(page_send):
element_inside_popup.send_keys(Keys.END)
time.sleep(5)
print(str(j))
#find all 'read more' button and click (code here)
else:
browser.implicitly_wait(12)
html = browser.page_source
bsObj_bd = BeautifulSoup(html,'html')
return bsObj_bd
我将 Selenium 与 Chrome 驱动程序一起使用。谢谢大家提前过来帮忙
使用 wait
和 try-except
检查 'read more' 按钮是否存在。
有关 wait
的更多信息,请参阅 this link。
#find all 'read more' button and click (code here)
from selenium.webdriver.support import expected_conditions as EC
try:
buttons = WebDriverWait(driver, 10).until(
EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='_yzu7qn']//button[@class='_ejra3kg']")))
except TimeoutException:
print("no read more")
else:
for button in buttons:
button.click()
我想获取客人的所有评论。但是有些评论的文字很长,需要单击 'read more' 按钮才能查看所有文字。问题是按钮的数量取决于评论。我可以加载并获得所有评论,它工作正常,但我不知道 'read more' 按钮。如何在模式
中单击所有 'read more' 按钮enter image description here
Link URL: https://th.airbnb.com/rooms/27194960/reviews?source_impression_id=p3_1600195106_a%2FYGw9bddHf%2BMfUE
下面的代码是从 URL 获取 HTML 文本的函数。有 2 个条件,我们将重点关注条件 2,它会获得您可以在 if-else 条件下看到的评论 --- if review:.
def get_pageswithSelenium(roomid,review,page_send):
#session = requests.Session()
#ua = UserAgent()
#headers = {'User-Agent':ua.random}
if not(review):
url = "https://th.airbnb.com/rooms/{}?source_impression_id=p3_1600195106_a%2FYGw9bddHf%2BMfUE".format(roomid)
else:
url = "https://th.airbnb.com/rooms/{}/reviews?source_impression_id=p3_1600195106_a%2FYGw9bddHf%2BMfUE".format(roomid)
print("selenium url: "+url)
browser = webdriver.Chrome(executable_path=r"C:\chromedriver_win32\chromedriver.exe")
browser.get(url)
if review:
browser.implicitly_wait(20)
element_inside_popup = browser.find_element_by_xpath('//div[@class="_yzu7qn"]//a')
for j in range(page_send):
element_inside_popup.send_keys(Keys.END)
time.sleep(5)
print(str(j))
#find all 'read more' button and click (code here)
else:
browser.implicitly_wait(12)
html = browser.page_source
bsObj_bd = BeautifulSoup(html,'html')
return bsObj_bd
我将 Selenium 与 Chrome 驱动程序一起使用。谢谢大家提前过来帮忙
使用 wait
和 try-except
检查 'read more' 按钮是否存在。
有关 wait
的更多信息,请参阅 this link。
#find all 'read more' button and click (code here)
from selenium.webdriver.support import expected_conditions as EC
try:
buttons = WebDriverWait(driver, 10).until(
EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='_yzu7qn']//button[@class='_ejra3kg']")))
except TimeoutException:
print("no read more")
else:
for button in buttons:
button.click()