首页 > 解决方案 > 如果子href符合要求,如何单击父类

问题描述

嗨,如果类中存在以下要求,我已经厌倦了找到正确的 selenium 代码来单击主父类:

Parent Class

<div class ="col-xs-2-4 shopee-search-item-result__item" data-sqe="item">

Child class

<a data-sqe="link" href= all urls that is printed in python.>

Child class contains this element
<div class = "_1gkBDw _2O43P5">
<div class = "_1HvBLA">
<div class = "_3ao649" data-sqe="ad"> Ad</div>


在此处输入图像描述

这是下面的代码

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

import time
url = 'https://shopee.com.my/search?keyword=mattress'

driver = webdriver.Chrome(executable_path=r'E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe')
driver.get(url)
time.sleep(0.8)

# select language
driver.find_element_by_xpath('//div[@class="language-selection__list"]/button').click()
time.sleep(3)

# scroll few times to load all items 
for x in range(10):
    driver.execute_script("window.scrollBy(0,300)")
    time.sleep(0.1)

# get all links (without clicking)

all_items = driver.find_elements_by_xpath('//a[@data-sqe="link"]')
print('len:', len(all_items))

all_urls = []

j = 0
k = 45

for item in all_items:
    url = item.get_attribute('href')
    all_urls.append(url)

print(all_urls)

a= len(all_urls)



# now use links
i = 0


while i <= 4 :
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-xs-2-4 shopee-search-item-result__item' and @data-sqe='item']//a[@class='link' and @href= all_urls[i]]"))).click()
    i+=1

我试图找到:-Div 整个类-定位类和 href 单独-单击前五列,但总是失败。

Traceback (most recent call last):
  File "E:/Users/Asashin/Desktop/Bots/click test 7.py", line 52, in <module>
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-xs-2-4 shopee-search-item-result__item' and @data-sqe='item']//a[@class='link' and @href= all_urls[i]]"))).click()
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

我能解决吗?

标签: pythonhtmlcssseleniumselenium-webdriver

解决方案


我做了几个改变。

  1. 当您获取href值时,您将获得完整的 url,而不是您在 DOM 中看到的 url,因此您需要删除前面的值以便稍后进行验证。

  2. 在最后一个 while 循环all_urls[i]是变量中,您需要将其作为变量而不是字符串传递。

  3. 单击每个链接后,您需要再次使用driver.back()

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
url = 'https://shopee.com.my/search?keyword=mattress'

driver = webdriver.Chrome(executable_path=r'E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe')
driver.get(url)
# select language
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,'//div[@class="language-selection__list"]/button'))).click()
time.sleep(3)

# scroll few times to load all items
for x in range(10):
    driver.execute_script("window.scrollBy(0,300)")
    time.sleep(0.1)

# get all links (without clicking)
all_items = driver.find_elements_by_xpath('//a[@data-sqe="link"]')
print('len:', len(all_items))

all_urls = []

j = 0
k = 45

for item in all_items:
    # This give you whole url of the anchor tag
    url = item.get_attribute('href')
    # You need to remove the preceding values in order to verify href later for clicking
    urlfinal=url.split('https://shopee.com.my')[1]
    all_urls.append(urlfinal)

print(all_urls)
a= len(all_urls)

# now use links
i = 0

while i <= 4 :
    #Identify the parent tag by child tag use following Xpath.
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-xs-2-4 shopee-search-item-result__item' and @data-sqe='item'][.//a[@data-sqe='link' and @href='" + all_urls[i] +"']]"))).click()
    driver.back()
    i+=1

推荐阅读