如何通过Selenium点击一个元素Python
How to click on a element through Selenium Python
我正在尝试使用 selenium 浏览器获取 facebook 帐户的数据 python 但无法找到我可以通过单击导出按钮查找的元素。
见附件截图
我试过了,但它似乎给了我 class.
的错误
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)
你能告诉我如何点击“导出”按钮吗?
到 运行 在 facebook、youtube 等应用程序上使用自动化脚本非常困难,因为它们是庞大的公司,并且他们的 Web 应用程序是由世界上最好的开发人员开发的,但 运行 自动化脚本并非不可能有时元素是动态生成的,有时是隐藏的或不活动的,你不能直接去点击
一个解决方案是您可以通过 xpath realtive 或 absolute 的单击操作来完成它们在资源文件中未指定为 "export_button" 我认为这可能对您有所帮助
您还可以通过 class 名称或 css 选择器找到元素,正如我在屏幕截图中看到的 class 名称存在“_271K _271m _1qjd layerConfirm”您可以执行单击操作那个
好吧,我可以使用 xpath 解决它。
这是解决方案
self.driver.find_element_by_xpath("//*[contains(@class, '_271k _271m _1qjd layerConfirm')]").click()
文本为 Export 的元素是一个动态生成的元素,因此要定位您必须为 WebDriverWait 引入的元素24=]元素可点击,您可以使用 :
使用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
我正在尝试使用 selenium 浏览器获取 facebook 帐户的数据 python 但无法找到我可以通过单击导出按钮查找的元素。
见附件截图
我试过了,但它似乎给了我 class.
的错误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)
你能告诉我如何点击“导出”按钮吗?
到 运行 在 facebook、youtube 等应用程序上使用自动化脚本非常困难,因为它们是庞大的公司,并且他们的 Web 应用程序是由世界上最好的开发人员开发的,但 运行 自动化脚本并非不可能有时元素是动态生成的,有时是隐藏的或不活动的,你不能直接去点击
一个解决方案是您可以通过 xpath realtive 或 absolute 的单击操作来完成它们在资源文件中未指定为 "export_button" 我认为这可能对您有所帮助
您还可以通过 class 名称或 css 选择器找到元素,正如我在屏幕截图中看到的 class 名称存在“_271K _271m _1qjd layerConfirm”您可以执行单击操作那个
好吧,我可以使用 xpath 解决它。 这是解决方案
self.driver.find_element_by_xpath("//*[contains(@class, '_271k _271m _1qjd layerConfirm')]").click()
文本为 Export 的元素是一个动态生成的元素,因此要定位您必须为 WebDriverWait 引入的元素24=]元素可点击,您可以使用
使用
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