0

So I've tried almost every method that I knew of including xpath and nothing. It should be a simple find element and click and that's it, but I can't figure it out.

enter image description here

Here is my code

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

ff_options = Options()

#profile
binary = FirefoxBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
profile = webdriver.FirefoxProfile('C:\\Users\\bravoj\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\7k4o5psw.CCC Deafualt')
ff_driver = webdriver.Firefox(executable_path='C:\\Users\\bravoj\Downloads\\geckodriver.exe')
#fire fox driver
ff_driver.get('about:profiles')
#ff_driver.get('about:preferences#home')

ff_driver.find_element_by_id('profiles-set-as-default').click()

There is a duplicate code too enter image description here

Jbravo
  • 43
  • 6

2 Answers2

2

'profiles-set-as-default' is not id attribute, it's data-l10n-id attribute. You can use css_selector to locate it, and get unique element using previous brother element with data-l10n-args {CCC Deafult}

ff_driver.find_element_by_css_selector('[data-l10n-args*="CCC Deafult"] ~ [data-l10n-id="profiles-set-as-default"]')
Guy
  • 46,488
  • 10
  • 44
  • 88
  • This actually worked. but It kept choosing the second profile instead of the 3rd one I was aiming for, the reason for this is because the first one also has the same code set as the element. so the code will run and find tat first one. Is there any way to have it skip that and find the second one. I have provided a picture – Jbravo Nov 20 '19 at 19:26
0

The element with text as Set as default profile is a dynamic element so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.tab#profiles button[data-l10n-id='profiles-set-as-default']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-l10n-id='profiles-set-as-default' and text()='Set as default profile']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352