How to extract the dynamic element id with random string using Selenium

144 views Asked by At

im using Selenium with python, im trying to enter value to the textarea with send_keys, but I can't get the id, because the id contain with random string

<div class="XiG xcv L4E zI7 iyn Hsu">
    <textarea class="TextArea__textArea TextArea__bold TextArea__enabled TextArea__large TextArea__wrap" id="pin-draft-title-7cd7d98c-0494-4cbb-98e3-12658ba8e175" placeholder="Add your title" rows="1" style="height: 65px;">
    </textarea>
    
    <div class="MIw QLY Rym ojN p6V zI7 iyn Hsu" style="pointer-events: none;">
    <div class="TextArea__textArea TextArea__dark TextArea__bold TextArea__large TextArea__wrap">
    </div>
    </div>
    </div>
    <div class=""><div class="xcv L4E zI7 iyn Hsu" style="box-shadow: rgba(142, 142, 142, 0.5) 0px 1px 0px 0px; padding-top: 8px;">
    </div>
    <div class="hDW xcv L4E zI7 iyn Hsu" style="min-height: 15px;"></div>
</div>

im trying to get the id below

id="pin-draft-title-7cd7d98c-0494-4cbb-98e3-12658ba8e175"

Here my send_keys code

self.send_keys( # Input a title. '//textarea[contains(@id, "pin-draft-title-")]', data.title)

i have already try use contains and starts-with its seems doesnt work

I got this error

Stacktrace:
Backtrace:
    Ordinal0 [0x0021ACD3+2075859]
    Ordinal0 [0x001AEE61+1633889]
    Ordinal0 [0x000AB7BD+571325]
    Ordinal0 [0x000DAC2F+764975]
    Ordinal0 [0x000DAE1B+765467]
    Ordinal0 [0x0010D0F2+970994]
    Ordinal0 [0x000F7364+881508]
    Ordinal0 [0x0010B56A+963946]
    Ordinal0 [0x000F7136+880950]
    Ordinal0 [0x000CFEFD+720637]
    Ordinal0 [0x000D0F3F+724799]
    GetHandleVerifier [0x004CEED2+2769538]
    GetHandleVerifier [0x004C0D95+2711877]
    GetHandleVerifier [0x002AA03A+521194]
    GetHandleVerifier [0x002A8DA0+516432]
    Ordinal0 [0x001B682C+1665068]
    Ordinal0 [0x001BB128+1683752]
    Ordinal0 [0x001BB215+1683989]
    Ordinal0 [0x001C6484+1729668]
    BaseThreadInitThunk [0x753F00F9+25]
    RtlGetAppContainerNamedObjectPath [0x77577BBE+286]
    RtlGetAppContainerNamedObjectPath [0x77577B8E+238]
    (No symbol) [0x00000000]

please help

2

There are 2 answers

0
undetected Selenium On BEST ANSWER

To extract the value of the id attribute i.e. pin-draft-title-7cd7d98c-0494-4cbb-98e3-12658ba8e175 you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "textarea.TextArea__textArea.TextArea__bold.TextArea__enabled.TextArea__large.TextArea__wrap[placeholder='Add your title']"))).get_attribute("id"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//textarea[@class='TextArea__textArea TextArea__bold TextArea__enabled TextArea__large TextArea__wrap' and @placeholder='Add your title']"))).get_attribute("id"))
    
  • 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
    

You can find a relevant discussion in Python Selenium - get href value

1
Shawn On

Try the below XPath expression:

//textarea[@class='TextArea__textArea TextArea__bold TextArea__enabled TextArea__large TextArea__wrap']

Full code to locate using XPath would look like this:

element = driver.find_element(By.XPATH, "//textarea[@class='TextArea__textArea TextArea__bold TextArea__enabled TextArea__large TextArea__wrap']")
element.send_keys("Your text here")