2

I want to click on "create post" button of this element:

<div class="l61y9joe j8otv06s a1itoznt qwtvmjv2 kiex77na lgsfgr3h mcogi7i5 ih1xi9zn ippphs35 a53abz89" data-hover="tooltip" data-tooltip-display="overflow" id="js_2f">Create Post</div>

But this id (id="js_2f") has a random value:

I did it this way but didn't work !

wait = WebDriverWait(self.browser, 20)
wait.until(EC.element_to_be_clickable((By.XPATH,  "//*[text()='Create Post' and contains(@id, 'js_')]"))).click()

How can i click on it Using Xpath method ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

1

Following should help:

driver.find_element_by_xpath('//div[contains(text(),"Create Post")]').click()
yrnr
  • 71
  • 6
0

You can try below xpath with contains

//div[contains(text(),'Create Post')]
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
0

You can try to perform a click on this element using link text

driver.find_element_by_link_text("Create Post")

or

driver.find_element_by_xpath("//div[text() = 'Create Post']")
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
  • please check again my post i did some changes, because there somthings i already did – Ayoub Developer Jul 16 '20 at 12:53
  • provide the full error log, for understanding the problem – Norayr Sargsyan Jul 16 '20 at 12:55
  • 'File "C:/Users/jhon doe/PycharmProjects/FilesLibrary/AutomateFCB_INST.py", line 29, in upload_FCB wait.until(EC.element_to_be_clickable((By.XPATH, "//*[text()='Create Post' and contains(@id, 'js_')]"))).click() File "C:\Python37\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: ' – Ayoub Developer Jul 16 '20 at 13:07
  • did you try the solutions which I mentioned in my answer? Please try and if the error still exists, provide this error only – Norayr Sargsyan Jul 16 '20 at 13:09
  • I got this error for both `selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Create Post"} ---------------------------- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(text(),"Create Post")]"}` – Ayoub Developer Jul 16 '20 at 20:31
  • i got this error after some changes ! `selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
    ...
    is not clickable at point (277, 296). Other element would receive the click:
    `
    – Ayoub Developer Jul 16 '20 at 21:35
0

The Create Post button is a JavaScript enabled element, so to 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, "input[data-hover='tooltip'][data-tooltip-display]"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-hover='tooltip' and text()='Create Post'][@data-tooltip-display]"))).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
  • didnt work , i got this error message `obj.upload_FCB() File "C:/Users/jhon doe/PycharmProjects/FilesLibrary/AutomateFCB_INST.py", line 31, in upload_FCB (By.XPATH, "//div[@data-hover='tooltip' and text()='Create Post'][@data-tooltip-display]"))).click() File "C:\Python37\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:` – Ayoub Developer Jul 16 '20 at 20:47
  • i got this error after some changes `selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
    ...
    is not clickable at point (277, 296). Other element would receive the click:
    `
    – Ayoub Developer Jul 16 '20 at 21:33