【发布时间】:2019-10-06 03:17:53
【问题描述】:
我不仅试图将鼠标悬停在一个点上,而且一个接一个地悬停在多个点上。 这里的点表示每个用户的图像配置文件(每个页面有 5 个)。 我这样做的原因是我尝试解析每个用户的链接配置文件。 但棘手的部分是 html 代码是隐藏的。换句话说,除非我将鼠标悬停在每个用户的个人资料或图片上,否则它不会显示。 让我直接跳到我的代码。
from selenium.webdriver import ActionChains
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from bs4 import BeautifulSoup
from selenium import webdriver
#Incognito Mode
option=webdriver.ChromeOptions()
option.add_argument("--incognito")
#Open Chrome
driver=webdriver.Chrome(executable_path="C:/Users/chromedriver.exe",options=option)
#Get the link
driver.get("https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-or20-Groove_Stone_Getaway-Asheville_North_Carolina.html")
#This is the first time for me to use Xpath so please understand if there's something wrong with my code
profile=driver.find_element_by_xpath("//div[@class='mainContent']")
profile_pic=profile.find_element_by_xpath("//div[@class='ui_avatar large']")
ActionChains(driver).move_to_element(profile_pic).perform()
ActionChains(driver).move_to_element(profile_pic).click().perform()
profile_box=driver.find_element_by_xpath('//span//a[contains(@href,"/Profile/")]').get_attribute("href")
print (profile_box)
在这种情况下,如何将鼠标悬停在多个用户(具有相同 Xpath 代码)上?
=================================更新代码=========== ===============
num_page=0
#Incognito Mode
option=webdriver.ChromeOptions()
option.add_argument("--incognito")
#Open Chrome
driver=webdriver.Chrome(executable_path="C:/Users/chromedriver.exe",options=option)
driver.implicitly_wait(10)
#Type in URL you want to visit
driver.get("https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-Groove_Stone_Getaway-Asheville_North_Carolina.html")
driver.maximize_window()
time.sleep(5)
#loop over multiple pages.
for j in range(1,16,1):
time.sleep(5)
try:
#finds all the comments or profile pics
profile_pic= driver.find_elements(By.XPATH,"//div[@class='prw_rup prw_reviews_member_info_hsx']//div[@class='ui_avatar large']")
time.sleep(3)
for i in profile_pic:
#clicks all the profile pic one by one
ActionChains(driver).move_to_element(i).perform()
time.sleep(2)
ActionChains(driver).move_to_element(i).click().perform()
time.sleep(4)
#print the href or link value
profile_box=driver.find_element_by_xpath('//span//a[contains(@href,"/Profile/")]').get_attribute("href")
time.sleep(3)
print (profile_box)
except:
pass
#click the next button to go to the next page.
link = driver.find_element_by_link_text('Next')
#Another element is covering the element you are to click.
#You could use execute_script() to click on this.
driver.execute_script("arguments[0].click();", link)
#After a certain number of pages, use break function to escape from the loop.
num_page=num_page+1
if num_page==14:
break
感谢 Yosuva A,我可以解决如何将鼠标悬停在同一页面中的多个用户上并解析数据。我尝试更多地开发代码,以便循环访问多个页面(每个页面包含 5 个用户)。
我更新的代码肯定会遍历多个页面,但在某个随机点,代码只解析相同的用户配置文件链接。
这是我得到的输出示例:
https://www.tripadvisor.com/Profile/Cftra
https://www.tripadvisor.com/Profile/jessicarZ577PF
https://www.tripadvisor.com/Profile/BackPacker115730
https://www.tripadvisor.com/Profile/nanm
https://www.tripadvisor.com/Profile/kukimama
https://www.tripadvisor.com/Profile/ThreeColeys
https://www.tripadvisor.com/Profile/AlanS990
https://www.tripadvisor.com/Profile/S5227HKlisas
https://www.tripadvisor.com/Profile/H1493VRmatthewt
https://www.tripadvisor.com/Profile/H1493VRmatthewt
https://www.tripadvisor.com/Profile/H1493VRmatthewt
https://www.tripadvisor.com/Profile/H1493VRmatthewt
https://www.tripadvisor.com/Profile/H1493VRmatthewt
我认为我需要添加时间睡眠功能,所以将它们放在几行中,但仍然遇到同样的问题。有人可以帮助我吗?发生这种情况以及如何克服它? 谢谢你。
【问题讨论】:
-
我想我已经在另一篇文章中给你答案了?你试过吗?