【问题标题】:how to hover over multiple elements using Python如何使用 Python 将鼠标悬停在多个元素上
【发布时间】: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

我认为我需要添加时间睡眠功能,所以将它们放在几行中,但仍然遇到同样的问题。有人可以帮助我吗?发生这种情况以及如何克服它? 谢谢你。

【问题讨论】:

  • 我想我已经在另一篇文章中给你答案了?你试过吗?

标签: python selenium hover


【解决方案1】:

Python 示例

此代码将一张一张点击所有个人资料图片并打印 href 值。

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome('/usr/local/bin/chromedriver')  # Optional argument, if not specified will search path.
driver.implicitly_wait(15)

driver.get("https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-or20-Groove_Stone_Getaway-Asheville_North_Carolina.html");

#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']")

for i in profile_pic:
        #clicks all the profile pic one by one
        ActionChains(driver).move_to_element(i).perform()
        ActionChains(driver).move_to_element(i).click().perform()
        time.sleep(2)
        #print the href or link value
        profile_box=driver.find_element_by_xpath('//span//a[contains(@href,"/Profile/")]').get_attribute("href")
        print (profile_box)

driver.quit()

输出

https://www.tripadvisor.com/Profile/861kellyd
https://www.tripadvisor.com/Profile/JLERPercy
https://www.tripadvisor.com/Profile/rayn817
https://www.tripadvisor.com/Profile/grossla
https://www.tripadvisor.com/Profile/kapmem

Java 示例

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.tripadvisor.com/VacationRentalReview-g60742-d7951369-or20-Groove_Stone_Getaway-Asheville_North_Carolina.html");

        //finds all the comments or profiles
        List<WebElement> profile= driver.findElements(By.xpath("//div[@class='prw_rup prw_reviews_member_info_hsx']//div[@class='ui_avatar large']"));

        for(int i=0;i<profile.size();i++)
        {
            //Hover on user profile photo
            Actions builder = new Actions(driver);
            builder.moveToElement(profile.get(i)).perform();
            builder.moveToElement(profile.get(i)).click().perform();
            //Wait for user details pop-up
            WebDriverWait wait = new WebDriverWait(driver, 10);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span//a[contains(@href,'/Profile/')]")));
            //Extract the href value
            String hrefvalue=driver.findElement(By.xpath("//span//a[contains(@href,'/Profile/')]")).getAttribute("href");
            //Print the extracted value
            System.out.println(hrefvalue);
        }
        //close the browser
        driver.quit(); 

    }

}

【讨论】:

  • 非常感谢您的 python 代码。它工作得非常好。我学到了很多!谢谢。
  • 刚刚注意到您的代码有效,但有时它会连续抓取相同的用户链接。像这样的东西。 tripadvisor.com/Profile/JLERPercytripadvisor.com/Profile/rayn817tripadvisor.com/Profile/rayn817tripadvisor.com/Profile/rayn817
  • 我们可以编写代码以您想要的格式填充它
  • 我不确定这与我的计算能力或速度有关,但是当我在打印出 href 或链接值之前添加 time.sleep 函数时,它比以前效果更好。
  • 在设置时间睡眠功能后,它运行良好,因为您的代码会抓取所有 4 个配置文件链接,但最后第五个配置文件会出现问题。错误消息说无法找到 Xpath 元素,并且在 chrome 浏览器中,它没有单击最后一个元素,而是挂在那里。
猜你喜欢
  • 1970-01-01
  • 2011-11-08
  • 2020-01-03
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多