简体   繁体   English

如何通过Python使用ChromeDriver Chrome和Selenium在页面上打印类别链接?

[英]How to print the the category links on a page using ChromeDriver Chrome and Selenium through Python?

Using Python3 I am trying to get Chrome Webdriver and Selenium to identify the various 'Classifieds' categories on the webpage www.jtinsight.com and from there print out the category titles. 我正在尝试使用Python3使Chrome Webdriver和Selenium在www.jtinsight.com网页上标识各种“分类”类别,并从中打印出类别标题。 So far using the code below the best i can do is get it to print out the first two - 'All categories' and 'Cars(Private)'. 到目前为止,使用下面我能做的最好的代码来打印出前两个-“所有类别”和“汽车(私人)”。 I have identified that the html for these two is different to the others and tried a number of different lines of code which i have listed in the code commented out, but cant identify the right tag/class/xpath etc. Any help would be appreciated. 我已经确定这两个的html与其他HTML不同,并尝试了许多不同的代码行,这些代码行我已在代码中注释掉,但无法识别正确的标记/类/ 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()

This is really a timing issue. 这确实是一个时间问题。 If I added a "sleep(5)" before collecting categories, it found all 24. Interestingly, when I used a WebDriverWait instead, it would still only pull up 2 items. 如果我在收集类别之前添加了“ sleep(5)”,它会找到全部24个。有趣的是,当我使用WebDriverWait时,它仍然只能提取2个项目。 So, to force the driver to do a little more work I extended the xpath. 因此,为强制驱动程序执行更多工作,我扩展了xpath。 the following worked for me: 以下为我工作:

categories = WebDriverWait(driver, 10).until(
EC.visibility_of_all_elements_located((By.XPATH, '//div[@class="mainCatEntry"]/div[@class="Description"]')))

To identify the various Classifieds categories on the webpage https://www.jtinsight.com/JTIRA/JTIRA.aspx#!/main and print the category titles, eg All categories , Cars (Private) , etc, you need to scroll down a bit and induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following solution: 要在网页https://www.jtinsight.com/JTIRA/JTIRA.aspx#!/main上标识各种分类类别,并打印类别标题,例如, 所有类别汽车(私人)等,您需要向下滚动一点,并为WebIdr_of_all_elements_located visibility_of_all_elements_located()引入WebDriverWait ,您可以使用以下解决方案:

  • Code Block: 代码块:

     from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_argument('disable-infobars') driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\\Utility\\BrowserDrivers\\chromedriver.exe') driver.get("https://www.jtinsight.com/JTIRA/JTIRA.aspx#!/main") driver.execute_script("arguments[0].scrollIntoView(true);",WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='ng-scope' and text()='Classifieds']")))); print([elem.get_attribute("innerHTML") for elem in WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='mainCatEntry']//div[@class='Description']")))]) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过 Selenium 和 Python 使用 ChromeDriver 和 Chrome 登录 ebay 帐户 - Unable to signin into ebay account using ChromeDriver and Chrome through Selenium and Python SessionNotCreatedException:此版本的 ChromeDriver 仅支持 Chrome 版本 84 使用 ChromeDriver 和 Chrome 通过 Selenium 和 Python - SessionNotCreatedException: This version of ChromeDriver only supports Chrome version 84 using ChromeDriver and Chrome through Selenium and Python chromedriver 不再运行,因此 ChromeDriver 假设 Chrome 使用 Selenium 到 Python 发生崩溃错误 - chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed error using Selenium through Python 如何通过Chromedriver和Selenium启动Chrome浏览器 - How to start Chrome Browser through Chromedriver and Selenium selenium.common.exceptions.WebDriverException: Message: invalid session id using Selenium with ChromeDriver and Chrome through Python - selenium.common.exceptions.WebDriverException: Message: invalid session id using Selenium with ChromeDriver and Chrome through Python selenium.common.exceptions.WebDriverException: 消息:无法通过 Selenium Python 使用 ChromeDriver Chrome 连接到服务错误 - selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python 如何下载 XML 文件避免弹出这种类型的文件可能会通过 ChromeDriver 和 Chrome 在 Python 中使用 Selenium 损害您的计算机 - How to download XML files avoiding the popup This type of file may harm your computer through ChromeDriver and Chrome using Selenium in Python 如何使用 selenium python chromeDriver 向下滚动到动态页面的末尾 - How to scroll down to end of the dynamic page using selenium python chromeDriver 网站通过 Selenium 使用 ChromeDriver 和 Chrome 导航到无访问页面,可能受 Bot Protected - Website navigates to no-access page using ChromeDriver and Chrome through Selenium probably Bot Protected 使用Selenium ChromeDriver中的ChromeOptions和通过Python的Chrome,“选项”对象没有属性“ set_preference”错误 - 'Options' object has no attribute 'set_preference' error using ChromeOptions in Selenium ChromeDriver and Chrome through Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM