【问题标题】:How would I check to see if there are buttons on a webpage?如何检查网页上是否有按钮?
【发布时间】:2020-05-23 00:48:58
【问题描述】:

我正在创建一个机器人来检查 Catus Trails 270 是否已发布,然后为我购买它们。

我不知道如何检查按钮是否存在。我将寻找该按钮,然后如果它没有看到它,它会刷新页面并再次查看。

我以其他空气最大值为例,说明页面掉落时的外观。这是一个代码示例:谢谢!

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys

class catusBot:
    def __init__(self, name):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.nike.com/launch/t/air-max-2090-pure-platinum-amd?cp=usns_aff_nike_")
        if self.driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div/div[3]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/ul/li[11]/button") == True:
            self.driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div/div[3]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/ul/li[11]/button").click()
            self.driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div/div[3]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/div/button").click()
        else:
            self.driver.refresh()


my_bot = catusBot("Ethan Houseworth")

【问题讨论】:

  • 本网站旨在解决特定的编码问题。请更准确,它将帮助我们帮助您:您是否运行了上面的代码?在哪一行没有工作?

标签: python selenium web-scraping selenium-chromedriver


【解决方案1】:

如果找不到元素,find_element_by_* 方法将引发NoSuchElementException。你会想要处理它。

【讨论】:

  • 我的问题是我前面有一段时间 True 所以即使按钮在屏幕上它也只是保持刷新。
  • @ehouse 我在你的代码块中没有看到while True
猜你喜欢
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多