我正在尝试进行一个查询,扫描链接到 div 类“_1gkBDw _2O43P5”的 URL 并打印它。
这是编程:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
url = 'https://shopee.com.my/search?keyword=mattress'
driver = webdriver.Chrome(executable_path=r'E:/users/Asashin/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()
# 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)
a = WebDriverWait(driver,15).until(EC.visibility_of_all_elements_located((By.XPATH,'//div[@class="col-xs-2-4 shopee-search-item-result__item"]'and contain('[@class="_1gkBDw _2O43P5"]'))))
b = driver.find_element_by_xpath()
while a==True:
all_items= driver.find_elements_by_xpath('//a[@data-sqe="link"]')
print(all_items)
break
以下是我遇到的一些问题:
我不知道如何在 Python/Selenium 中搜索另一个 div 类中的 div 类:
WebDriverWait(driver,15).until(EC.visibility_of_all_elements_located((By.XPATH,'//div[@class="col-xs-2-4 shopee-search-item-result__item"]'and contain('[@class="_1gkBDw _2O43P5"]'))))
这是它提供的结果:
Traceback (most recent call last):
File "E:/Users/Francabicon/Desktop/Bots/click test 3.py", line 26, in <module>
a = WebDriverWait(driver,15).until(EC.visibility_of_all_elements_located((By.XPATH,'//div[@class="col-xs-2-4 shopee-search-item-result__item"]'and contain('[@class="_1gkBDw _2O43P5"]'))))
NameError: name 'contain' is not defined
这个问题有解决办法吗?
最佳答案
您似乎使用的是 contain
而不是 contains
。
尝试
//div[@class='hoge' and contains(@id,'huga')
关于python - Python/Selenium 中查询链接打印错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59488194/