如何使用 Selenium/Python 查找和比较具有样式属性的文本? [英] How to find and compare text with the style property using Selenium/Python?

查看:29
本文介绍了如何使用 Selenium/Python 查找和比较具有样式属性的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这个问题的措辞很奇怪,我刚开始使用 Selenium.我想制作一个程序,当批发网站上的某些口味的某些电子烟几乎缺货时,让某人知道.到目前为止,我的程序非常简单,将访问特定电子烟的 url 并从表格中打印出显示不同口味电子烟及其库存的文本.

Sorry if this question is worded weirdly I just started using Selenium. I want to make a program that lets someone know when certain flavors of certain vapes are almost out of stock on a wholesale website. So far, my program is really simple and will go the url of a specific vape and print out the text from the table showing different flavors of vapes and their stock.

但是,我想查看库存数量(剩余电子烟数量)下的文本并打印(这种特定的电子烟口味)几乎缺货!";如果数字低于 10.这是我目前的代码:

However, I want to check the text under stock quantity (the number of vapes left) and print "(this this specific vape flavor) is almost out of stock!" if the number is below 10. Here is my code so far:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://safagoods.com/vape-shop/disposable-vape-devices/huge-disposables")


element = driver.find_element_by_xpath("//*[@id='input-option2231']").text
print(element)
driver.close()

有什么建议吗?我不打算对网站上的每一个电子烟都这样做,只是使用非常受欢迎的电子烟,这样使用它的人就可以运行程序并了解他们应该先订购什么以及他们可以等待订购什么.谢谢各位.

Any tips? I don't plan on doing this with every single vape on the website, just the very popular ones so the person using it can run the program and get an idea of what they should order first and what they can wait to order. Thanks guys.

推荐答案

作为一个可能的解决方案,您可以检查该值是否小于 x,然后将其附加到您的列表中.我的解决方案会打印一份数量少于 20 的清单.

As a possible solution, you can check if the value is less then x and append it to your list afterwards. My solution will print you a list where the amount is less than 20.

解决方案

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')

driver.get("https://safagoods.com/vape-shop/disposable-vape-devices/huge-disposables")
wait = WebDriverWait(driver, 15)
table = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".table.table-bordered tr[style='background: #eeeeee;']")))
data = []
rows = driver.find_elements_by_css_selector(".table.table-bordered tr[style='background: #eeeeee;']")
for row in rows:
    #qty = row.find_element_by_xpath("./td[1]").text
    stock = row.find_element_by_xpath("./td[2]").text
    name = row.find_element_by_xpath("./td[3]").text
    if int(stock) < 20:
        data.append([stock, name])
print(*data, sep='\n')

输出:

['17', 'Banana Ice (SKU: HUGE-BI )']
['17', 'Lush Ice (SKU: HUGE-LI )']

你也可以不带括号打印:

You can also print it without brackets with:

for x in data:
    print(' '.join(x))

这会给你输出:

17 Banana Ice (SKU: HUGE-BI )
17 Lush Ice (SKU: HUGE-LI )

这篇关于如何使用 Selenium/Python 查找和比较具有样式属性的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆