硒可以点击不同的链接吗?
Can selenium click on diffrents links?
我想从此 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('\n')].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)")
UPD
这正是我 运行:
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('\n')[0]
print(product_size)
#get back to the main page
driver.execute_script("window.history.go(-1)")
它给我打印了 גודל: 100 ML
这样的产品尺寸
我想从此 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('\n')].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)")
UPD
这正是我 运行:
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('\n')[0]
print(product_size)
#get back to the main page
driver.execute_script("window.history.go(-1)")
它给我打印了 גודל: 100 ML