Python/Selenium: Not able to find dynamically-generated element (button), Error: "Element could not be found"
Question: I'm trying to post text and hyperlink combinations on multiple Facebook groups
for my online business promotion.

The problem with my code is: when I pass a hyperlink and some text to
`send_keys` and try to click a button, my script is not able to locate the
button and gives me an error: `element could not be found`.

When I try to pass text to `send_keys` without a hyperlink, I don't receive
this error.

Code:

    
    
    import re 
    from random import randint 
    import os 
    import time 
    import sys
    import csv 
    from selenium import webdriver from
    selenium.webdriver.common.keys import Keys from
    selenium.webdriver.support.ui import Select
    
    
    driver = webdriver.Firefox() driver.get("https://www.facebook.com/")
    driver.find_element_by_id("email").clear()
    driver.find_element_by_id("email").send_keys("abc@yahoo.in")
    driver.find_element_by_id("pass").clear()
    driver.find_element_by_id("pass").send_keys("************")
    driver.find_element_by_class_name("uiButton").click() time.sleep(3)
    
    with open('url1', 'r') as f:
        for line in f:
            time.sleep(randint(8,10))
            driver.get(line)
            try:
                driver.find_element_by_class_name("uiTextareaAutogrow").click()
            except:
                pass
            time.sleep(randint(3,6))
            driver.find_element_by_class_name("uiTextareaAutogrow").send_keys("some text and hyderlink http://google.com ")
            time.sleep(randint(10,15))
            driver.find_element_by_xpath("//*[@id='u_0_1o']/div/div[5]/div/ul/li[2]/button/").click()
    

Note: I have URLs of groups collected in a file.

Please advise how this problem can be solved.


Answer: Instead of `find_element_by_xpath` you can try to use `WebDriverWait`. Syntax
is something like this -

    
    
    WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, '<CSS HERE>'))).click()
    

This will basically try calling `find_element_by_css_selector` every half a
second until timeout.

