【问题标题】:How to click on a element through Selenium Python如何通过 Selenium Python 点击​​一个元素
【发布时间】:2019-06-09 03:44:55
【问题描述】:

我正在尝试使用 selenium 浏览器 python 获取 facebook 帐户的数据,但无法找到我可以在单击导出按钮时查看的元素。

见附件截图

我试过了,但它似乎给了我上课的错误。

def login_facebook(self, username, password):
    chrome_options = webdriver.ChromeOptions()
    preference = {"download.default_directory": self.section_value[24]}

    chrome_options.add_experimental_option("prefs", preference)
    self.driver = webdriver.Chrome(self.section_value[20], chrome_options=chrome_options)
    self.driver.get(self.section_value[25])

    username_field = self.driver.find_element_by_id("email")
    password_field = self.driver.find_element_by_id("pass")

    username_field.send_keys(username)
    self.driver.implicitly_wait(10)

    password_field.send_keys(password)
    self.driver.implicitly_wait(10)

    self.driver.find_element_by_id("loginbutton").click()
    self.driver.implicitly_wait(10)

    self.driver.get("https://business.facebook.com/select/?next=https%3A%2F%2Fbusiness.facebook.com%2F")
    self.driver.get("https://business.facebook.com/home/accounts?business_id=698597566882728")
    self.driver.get("https://business.facebook.com/adsmanager/reporting/view?act="
                    "717590098609803&business_id=698597566882728&selected_report_id=23843123660810666")
    # self.driver.get("https://business.facebook.com/adsmanager/manage/campaigns?act=717590098609803&business_id"
    #                 "=698597566882728&tool=MANAGE_ADS&date={}-{}_{}%2Clast_month".format(self.last_month,
    #                                                                                      self.first_day_month,
    #                                                                                      self.last_day_month))

    self.driver.find_element_by_id("export_button").click()
    self.driver.implicitly_wait(10)
    self.driver.find_element_by_class_name("_43rl").click()
    self.driver.implicitly_wait(10)

您能告诉我如何点击导出按钮吗?

【问题讨论】:

  • 你试过用“export”这个词搜索吗?
  • @QHarr 这是一种使用文本“self.driver.find_elements_by_link_text("Export").click()"的方式
  • 似乎仍然出现错误,无法确定找到正确关联类或其他内容的方法:(

标签: python-2.7 selenium xpath css-selectors webdriverwait


【解决方案1】:

在 facebook、youtube 等应用程序上运行自动化脚本相当困难,因为它们是巨大的公司,他们的网络应用程序是由世界上最好的开发人员开发的,但运行自动化脚本并非不可能,有时元素是动态生成的,有时是隐藏或不活动的不能随便点击

一个解决方案是您可以通过xpath realtive 或absolute 的点击操作来完成它们不是资源文件中指定为“export_button”的id 我认为这可能会对您有所帮助

您还可以按类名或 css 选择器查找元素,正如我在屏幕截图中看到的那样,类名存在“_271K _271m _1qjd layerConfirm”,您可以对其执行点击操作

【讨论】:

  • export_button 与您在我的代码中看到的不同按钮相关联。
  • @akshaypatil 您能否将同一问题的两个答案合并为一个规范答案并删除多余的答案?
【解决方案2】:

好吧,我可以通过使用 xpath 来解决它。 这是解决方案

self.driver.find_element_by_xpath("//*[contains(@class, '_271k _271m _1qjd layerConfirm')]").click()

【讨论】:

    【解决方案3】:

    文本为 Export 的元素是动态生成的元素,因此要定位该元素,您必须诱导 WebDriverWait 以使 元素可点击 你可以使用Locator Strategies中的任何一个:

    • 使用CSS_SELECTOR

      WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.layerConfirm>div[data-hover='tooltip'][data-tooltip-display='overflow']"))).click()
      
    • 使用XPATH

      WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'layerConfirm')]/div[@data-hover='tooltip' and text()='Export']"))).click()
      
    • 注意:您必须添加以下导入:

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

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 2020-04-09
      • 2020-04-28
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多