【问题标题】:Query link printing error in Python/SeleniumPython/Selenium 中的查询链接打印错误
【发布时间】:2020-04-16 16:23:17
【问题描述】:

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

这个问题有解决办法吗?

【问题讨论】:

    标签: python html selenium selenium-webdriver


    【解决方案1】:

    您似乎使用的是contain 而不是contains

    试试

    //div[@class='hoge' and contains(@id,'huga')
    

    【讨论】:

    • 即使我放置包含它仍然无法正常工作。
    • 我尝试创建各种 while 循环,但它似乎没有获取 URL 也找不到类
    【解决方案2】:

    您要查找的元素是具有classattribute _1gkBDw _2O43P5div 元素,位于具有col-xs-2-4 shopee-search-item-result__item 属性的div 的子元素中。

    您必须找到div[@ class =" col-xs-2-4 shopee-search-item-result__item "] 并使用// 语法,这是对其子项的完整搜索(div[@class="_1gkBDw _2O43P5"])。

    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)
    WebDriverWait(driver,15).until(EC.visibility_of_all_elements_located((By.XPATH,'//div[@class="col-xs-2-4 shopee-search-item-result__item"]//div[@class="_1gkBDw _2O43P5"]')))
    
    for element in driver.find_elements_by_xpath('//a[@data-sqe="link"]'):
        print(element)
    

    【讨论】:

    • 我试过它确实可以运行,但它没有输出任何东西
    【解决方案3】:

    获取所有包含div 元素和类_1gkBDw _2O43P5URLs

    归纳出WebDriverWaitvisibility_of_all_elements_located() 以及下面的XPath 表达式:

    "//div[@class='col-xs-2-4 shopee-search-item-result__item'][.//div[@class='_1gkBDw _2O43P5']]//a[@data- sqe='链接']"


    代码块:

    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) which contains div element with class `_1gkBDw _2O43P5`
    alllinks = WebDriverWait(driver,15).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[@class='col-xs-2-4 shopee-search-item-result__item'][.//div[@class='_1gkBDw _2O43P5']]//a[@data-sqe='link']")))
    
    for link in alllinks:
      # Print the href value of the anchor tag
      print(link.get_attribute("href"))
    

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 2019-10-18
      • 1970-01-01
      • 2021-06-26
      • 2021-07-06
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多