A guide on how to extract the element id containing a random string in Selenium

While utilizing Selenium with Python, I am attempting to input a value into the textarea using send_keys. However, I am facing difficulty in obtaining the id as it contains a 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>

I am attempting to retrieve the following id:

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

This is my send_keys code:

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

I have tried using 'contains' and 'starts-with' but they do not seem to work.

I encountered this error:

Stacktrace:
Backtrace:
    Ordinal0 [0x0021ACD3+2075859]
    Ordinal0 [0x001AEE61+1633889]
    ...

Please provide assistance. Thank you.

Answer №1

To retrieve the id attribute value, which is

pin-draft-title-7cd7d98c-0494-4cbb-98e3-12658ba8e175
, you'll need to use a WebDriverWait for the method visibility_of_element_located(). You can choose from these two 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).util(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 : Don't forget to import the following :

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

If you're interested, check out this discussion on Python Selenium - get href value

Answer №2

Here is a helpful XPath expression to locate the desired element:

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

To use this XPath, you can implement it in your code like so:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am looking to create a for loop that will automate the process of clicking on multiple links and navigating back to the previous page

Below is the code I have written to achieve a specific task: my_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//table[@border='1']//a"))) for option in my_list: option.click() WebDriver ...

Steps for retrieving the attribute values of <i> tags enclosed within a designated <div> element

Currently, I am extracting data from a website where I encounter a recurring pattern with a varying number of values. For example, an instance of this pattern is shown below: <div class="lang"> <i class="flag fr" qtip-tooltip= ...

Preventing overlapping of shapes controlled by mouse movements in QGraphicsItem

A thought-provoking conversation was initiated here regarding the issue of preventing collisions of circles, represented by QGraphicsEllipseItems, within a QGraphicsScene. While the question initially focused on 2 colliding items, the overarching inquiry p ...

Automating pagination with Selenium

Currently, I am experimenting with using selenium to automate pagination by navigating through "next" buttons. My goal is to apply this technique on a large scale, potentially spanning hundreds of pages. While I have successfully implemented the functional ...

A step-by-step guide on adjusting the font size of a Python pptx element

I'm facing an issue with changing the font size in my pptx presentation. I've attempted to adjust it using title_shape.font = Pt(15) and body_shape.font = Pt(10), but unfortunately it's not working. This is the code snippet I have been work ...

Python script to locate the identical sentence in two separate files

Hello there. I am a beginner in Python and need some help with finding code that can search for exact keywords in a text file within an HTML file. For example, looking for keywords from keyword.txt in data.html. Currently, the code is only matching the fir ...

Ways to execute a singular python function concurrently on multiple occasions

I am looking to run a Python function in parallel 100 times. Can anyone provide the code snippet to achieve this? from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium import webdriver from selenium.webdriver.chrome ...

Converting Ordinal Time to Date-Time using Python

I need help converting a specific ordinal time array (shown below) to date-time format (YYYY-MM-DD hh:mm:ss.sss) in Anaconda/Python 3. I have searched for solutions to this issue, but none take into account the decimal precision of the original data. The ...

Is it possible to adjust environment variables during runtime in a live production environment for a rails application?

I am currently developing a web application that focuses on automating CRUD tasks on Amazon. This includes the ability for users to delete and add addresses on their Amazon account. To automate these tasks, I am utilizing Selenium WebDriver with a Mozilla ...

Running code within the __init__ function

When working on my class project, I encountered a problem while trying to untar xz/bx2/gz files within the init section. The code snippet I am using for this task is as follows: class myClass(object): def __init__(self, *args): for i in args: ...

Exploring network performance using Chrome DevTools

I am currently using version 4.0.0-alpha5 of the Selenium Webdriver NuGet package. The code I have only seems to work when the DevTools are open in Chrome Version 98, which confuses me. It should technically work regardless, but it consistently only works ...

Squeezing and unsqueezing data dimensions in a Pythonic

When preparing the data for a PyTorch Model, I often use the squeeze and unsqueeze functions like so: inps = torch.FloatTensor(data[0]) tgts = torch.FloatTensor(data[1]) tgts = torch.unsqueeze(tgts, -1) tgts = torch.unsqueeze(tgts, -1) tgts ...

Press the "show more" button multiple times to display all information in the table and retrieve all data from the table

I am trying to extract all the data from a table on the following page: However, I keep clicking the "show more" button but the table remains limited to showing only 30 rows. import sys import time from pyvirtualdisplay import Display from selenium impor ...

Utilize Javascript to perform operations on ndb.key()

When working with my application, I encounter keys that are created within a Python system and written to Google Datastore. To access the corresponding data, I require the IDs of these items, which are contained in the 'key' string. Unfortunate ...

Error encountered in Python 2.7 with Selenium 2: 'NoneType' object does not possess the attribute 'group'

Having recently delved into the world of Python/Selenium, I've been encountering a persistent roadblock while attempting to adjust some code. Despite my efforts scouring the internet and experimenting with various tweaks for days on end, I find myself ...

ValueError: The target checking process encountered an issue as it was expecting the activation_6 to have a shape of (70,), but instead received an array with a

I have been working on developing a face recognition system using CNN. I followed a tutorial and used Tensorflow==1.15 for this project. The program is designed to capture 70 images of the user's face and store them in a folder named 'dataset&ap ...

Navigating around Security Certificate page on Microsoft Edge using Selenium WebDriver

Is there a way to bypass the Security Certificate page for Microsoft Edge when using Selenium Webdriver with Python? https://i.stack.imgur.com/StSpB.jpg I've tried solutions that have worked for Internet Explorer in the past, following suggestions f ...

Python - when there is a lengthy loop running, how can a thread be used to periodically perform an action within the same scope?

Imagine a scenario where I am running a loop that involves reading a lengthy file. How can I ensure that at regular intervals, let's say every x milliseconds, a specific "some code" is executed? while inFile.readable(): line = inFile.readline() ...

go through each column iteratively

I am a beginner when it comes to working with pandas, still in the learning phase. I have a question regarding accessing columns using iloc[:,1]. If this syntax is used to locate a column by index, how can I access multiple columns at once, specifically co ...

Error: Unable to locate elements due to a null XPath expression. This issue arises while attempting to input keys using a Method

Encountering an error when using Sendkeys in methods. public static void inputTask(String task) throws Exception { // Entering task name GUIFunctions.typeTxtboxValue(driver,By.xpath(ObjRepoProp.getProperty("inputTaskName_XPATH")),task); Thread ...