0

Im trying to upload an image to facebook and im unable to click on the Add photo and video button.

When im looking at the html this is the element im trying to click:

<input aria-label="Add Photo or Video" accept="video/*,  video/x-m4v, 
video/webm, video/x-ms-wmv, video/x-msvideo, video/3gpp, video/flv, 
video/x-flv, video/mp4, video/quicktime, video/mpeg, video/ogv, .ts, .mkv, 
image/*, image/heic, image/heif" containerclassname="_5g_r" multiple="" 
name="composer_photo[]" display="inline" role="button" tabindex="0" data- 
testid="media-sprout" type="file" class="_n _5f0v" id="js_17y">

im trying to find the elment by id:

driver.find_elment_by_id("js_17y").click()

and im getting:

selenium.common.exceptions.NoSuchElementException: Message: no such element
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Udi
  • 67
  • 8
  • please check the element id is constant always and also, check the element is inside frame. – Murthi Jun 20 '19 at 10:34

3 Answers3

3

Facebook is built through ReactJS so to click() on the element you need 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, "input[aria-label='Add Photo or Video'][name^='composer_photo'][data-testid='media-sprout']"))).click()
    
  • Using xpath:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-label='Add Photo or Video' and starts-with(@name, 'composer_photo')][@data-testid='media-sprout']"))).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
0

your id might getting change everytime.Try use xpath with attribute. However use webdriverwait and element_to_be_clickable to click on that.

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

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//input[@aria-label="Add Photo or Video"][@name="composer_photo[]"]'))).click() 
KunduK
  • 32,888
  • 5
  • 17
  • 41
-2

Thanks guys i was able to find it by the class name trough the parent

driver.find_element_by_class_name("_3jk")
Udi
  • 67
  • 8