2

I'm trying to fetch data for facebook account using selenium browser python but can't able to find the which element I can look out for clicking on an export button.

See attached screenshotenter image description here

I tried but it seems giving me an error for the 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)

Can you please let me know how can i click on Export button?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
MishD
  • 1,470
  • 1
  • 9
  • 27
  • possible duplicate of https://stackoverflow.com/questions/35531069/find-submit-button-in-selenium-without-id – Kryesec Jan 15 '19 at 06:32
  • Have you tried searching by the text "export" ? – QHarr Jan 15 '19 at 07:09
  • @QHarr is this a way to use a text "self.driver.find_elements_by_link_text("Export").click()" – MishD Jan 15 '19 at 07:14
  • Seems still getting error, Can't able to identify a way to find the right associated class or something else :( – MishD Jan 15 '19 at 07:14

3 Answers3

3

The element with text as Export is a dynamically generated element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the Locator Strategies:

  • Using 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()
    
  • Using XPATH:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'layerConfirm')]/div[@data-hover='tooltip' and text()='Export']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
1

Well, I'm able to resolve it by using xpath. Here is the solution

self.driver.find_element_by_xpath("//*[contains(@class, '_271k _271m _1qjd layerConfirm')]").click()
MishD
  • 1,470
  • 1
  • 9
  • 27
0

to run automation scripts on applications like facebook, youtube quite a hard because they are huge coporations and their web applications are developed by the worlds best developers but its not impossible to run automation scripts sometimes elements are generated dynamically sometimes hidden or inactive you cant just go and click

one solution is you can do by click action by xpath realtive or absolute their is not id specified as "export_button" in resource file i think this might help you

you can also find element by class name or css selector as i see in screen shot the class name is present "_271K _271m _1qjd layerConfirm " you can perform click action on that

akshay patil
  • 656
  • 4
  • 19
  • export_button is associated with the different button as you can see in my code. – MishD Jan 15 '19 at 06:56
  • @akshaypatil Can you club up two of your answers to the same question into a single canonical answer and delete the redundant one? – DebanjanB Jan 15 '19 at 09:02