Selify可以点击不同的链接吗? [英] Can selenium click on diffrents links?

查看:28
本文介绍了Selify可以点击不同的链接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除此website中的数据(向下滚动时忽略它加载的香水)。

对于每种香水,我都想知道它的大小。为了查看它的大小,我需要点击将我带到另一个页面的香水。 假设当我在香水的url中时,我可以得到香水的大小,我如何才能编写一个程序来给我提供网站中每种香水页面的url?

这是当我有正确的url时查找香水大小的代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

urlM = 'https://www.myperfume.co.il/155567-%D7%9B%D7%9C-%D7%94%D7%9E%D7%95%D7%AA%D7%92%D7%99%D7%9D-%D7%9C%D7%92%D7%91' 
       '%D7%A8?order=up_title&page=0'
scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
         "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
client = gspread.authorize(creds)

spreadsheet = client.open("Perfumes")

options = ChromeOptions()
options.headless = True
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

driver.get(# [THE PERFUME'S URL]... )
info = driver.find_element_by_xpath('//*[(@id = "item_current_sub_title")]//span').text
res = ''
for i in info[:info.find('
')].replace('גודל', ''):
    if i.isdigit() or i.isalpha():
        res += i
print(res)

推荐答案

此处您需要以下内容:
根据每个产品,将鼠标悬停在要生产的产品上,将显示更多详细信息和添加到购物车按钮。
单击";更多详细信息";按钮。
在打开的页面中获取产品尺寸(以及任何其他详细信息)。
返回主页。
为了对许多产品执行此操作,您必须在主页上再次获得产品列表。否则将出现陈旧元素异常。
因此,您的代码可能如下所示:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

actions = ActionChains(driver)
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'layout_list_item')]")))
time.sleep(1)
products = driver.find_elements_by_xpath("//div[contains(@class,'layout_list_item')]")
for i in range(len(products)):
    wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'layout_list_item')]")))
    time.sleep(1)
    product = driver.find_elements_by_xpath("//div[contains(@class,'layout_list_item')]")[i]
    #hover over the product block
    actions.move_to_element(product).perform()
    #click the "mode details button
    product.find_element_by_xpath(".//p[contains(@class,'extra_button')]").click()
    #in the details page get the product sub-title containing the product size
    product_size = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div#item_current_sub_title"))).text
    #get back to the main page
    driver.execute_script("window.history.go(-1)")

更新
这正是我所运行的:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time

urlM = 'https://www.myperfume.co.il/155567-%D7%9B%D7%9C-%D7%94%D7%9E%D7%95%D7%AA%D7%92%D7%99%D7%9D-%D7%9C%D7%92%D7%91' 
       '%D7%A8?order=up_title&page=0'

driver = webdriver.Chrome(executable_path='chromedriver.exe')
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)


driver.maximize_window()

driver.get(urlM)
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'layout_list_item')]")))
time.sleep(1)
products = driver.find_elements_by_xpath("//div[contains(@class,'layout_list_item')]")
for i in range(len(products)):
    wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'layout_list_item')]")))
    time.sleep(1)
    product = driver.find_elements_by_xpath("//div[contains(@class,'layout_list_item')]")[i]
    #hover over the product block
    actions.move_to_element(product).perform()
    #click the "mode details button
    product.find_element_by_xpath(".//p[contains(@class,'extra_button')]").click()
    #in the details page get the product sub-title containing the product size
    product_size = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div#item_current_sub_title"))).text
    product_size = product_size.split('
')[0]
    print(product_size)
    #get back to the main page
    driver.execute_script("window.history.go(-1)")

它会打印出产品的尺寸,如גודל: 100 ML

这篇关于Selify可以点击不同的链接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆