Py学习  »  Python

如何让Chrome Webdriver和Selenium在python中识别页面上的类别链接?

JamesLC • 4 年前 • 607 次点击  

使用Python3,我试图让Chrome Webdriver和Selenium在www.jtinsight.com网页上识别各种“分类”类别,并从中打印出类别标题。到目前为止,使用下面的代码,我能做的就是让它打印出前两个-“所有类别”和“汽车(私人)”。我已经确定这两个html与其他html不同,并尝试了许多不同的代码行,我在代码中列出了注释,但无法识别正确的标记/class/xpath等。 任何帮助都将不胜感激。

from selenium import webdriver
from selenium.webdriver.common.by import By

# Creating the WebDriver object using the ChromeDriver
driver = webdriver.Chrome()

# Directing the driver to the defined url
driver.get("https://www.jtinsight.com/JTIRA/JTIRA.aspx#!/main")

# Locate the categories

# Each code line runs but only returns the first two categories
# categories = driver.find_elements_by_xpath('//div[@class="col-md-3 col-sm-4 col-xs-6"]')
# categories = driver.find_elements_by_xpath('//div[@class="mainCatEntry"]')
# categories = driver.find_elements_by_xpath('//div[@class="Description"]')

# Process ran but finished with exit code 0
# categories = driver.find_elements_by_xpath('//*[@class="col-md-3 col-sm-4 col-xs-6 ng-scope"]')
# categories = driver.find_elements_by_xpath('//div[@class="col-md-3 col-sm-4 col-xs-6 ng-scope"]')
# categories = driver.find_elements_by_partial_link_text('//href[@class="divLink"]')
# categories = driver.find_elements_by_tag_name('bindonce')
# categories = driver.find_elements_by_xpath('//div[@class="divLink"]')

# Error before finished running
# categories = driver.find_elements(By.CLASS_NAME, "col-md-3 col-sm-4 col-xs-6 ng-scope")
# categories = driver.find_elements(By.XPATH, '//div bindonce[@class="col-md-3 col-sm-4 col-xs-6 ng-scope"]')
# categories = driver.find_elements_by_class_name('//div bindonce[@class="col-md-3 col-sm-4 col-xs-6 ng-scope"]')

# Print out all categories on current page
num_page_items = len(categories)
print(num_page_items)
for i in range(num_page_items):
    print(categories[i].text)

# Clean up (close browser once task is completed.)
driver.close()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51231
 
607 次点击  
文章 [ 2 ]  |  最新文章 4 年前