python - Python/Selenium 中查询链接打印错误

标签 python html selenium selenium-webdriver

我正在尝试进行一个查询,扫描链接到 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/

相关文章:

html - Zurb Foundation 6 不适用于 Codeigniter

html - 修复了居中博文旁边的侧边栏

python - 从字节文件打开 PIL 图像

python - 意外异常 : name 'basestring' is not defined when invoking ansible2

python - matplotlib 程序运行时窗口无响应

html - 是自动对焦 ="autofocus"还是自动对焦?

python - 日期时间对象上的 Django F 表达式

testing - 如何仅在 Selenium 的特定 div 中单击按钮?

c# - 在 anchor 标记内使用 Selenium 选择图像按钮

python - 在 Windows 8 上安装并运行 Django