0

I have to select elements which have dynamic "id" and "name" selectors, changing everytime the page is reloaded. I cant use css selectors either because the elements I am targeting share common selectors. The following is the html for the two elements that i would like to select:

<td colspan="60" width="60%" class="cell-right" valign="top">
  <textarea onkeydown="event.cancelBubble=true;" onkeypress="event.cancelBubble=true;" onkeyup="event.cancelBubble=true;" name="1c2b0730969c468aa06b312d96f1513b" id="1c2b0730969c468aa06b312d96f1513b" class="auto-size-field" rows="5"></textarea>
</td>

and

<td colspan="60" width="60%" class="cell-right" valign="top">
  <textarea onkeydown="event.cancelBubble=true;" onkeypress="event.cancelBubble=true;" onkeyup="event.cancelBubble=true;" name="6c5c8088fb474f2b93e1de9fb1b1771d" id="6c5c8088fb474f2b93e1de9fb1b1771d" class="auto-size-field" rows="5"></textarea>
</td>

Both the 'id' and 'name' selectors change dynamically everytime the page is reloaded. The xpaths are also changing on each reload as the contain references to the 'id' tags (ex. '//*[@id="6c5c8088fb474f2b93e1de9fb1b1771d"]'). And as you can see, these two elements share the same css selectors, which further complicates the matter...

The question at this point is how would I go about selecting them individually so I can interact with each text-area using send_keys()?

1
  • Can you add the HTML for the search bar? Why can't you use any css locator? Commented May 24, 2017 at 4:08

2 Answers 2

1

Its a timing issue, add implicit wait to wait for the element to exist while using find_element. You need to define it one time after the driver initialization, it will apply for the rest of the driver "life time"

In addition, the search bar you are looking for has id and name q

from selenium import webdriver

driver = webdriver.Chrome("C:/Users/xxx/xxx/chromedriver/chromedriver.exe")
driver.implicitly_wait(5) # seconds

login_url = "http://www.alluc.ee"
driver.get(login_url)

search_bar = driver.find_element_by_id('q')
# or search_bar = driver.find_element_by_name('q')
Sign up to request clarification or add additional context in comments.

4 Comments

Hi guy thanks for your input, but this isnt really what i am looking for. See i must use "absolute" xpaths rather than id selectors. The reason is that i'm trying to get this to work on a page where "id"s are dynamic (changing everytime the page is reloaded)
@LawlessLeopard in that case you should provide concrete example. Using absolute xpath is generally bad idea, and there are many other ways to locate elements besides id.
as per your recommendation i just changed the question to include a concrete example. The reason as to why i was attempting to use absolute xpaths, is that the elements i want to select share the same css-selectors and have dynamic "id" and "name" tags which change everytime the page is reloaded. Any suggestions as to how to select these?
@LawlessLeopard It might be possible to locate them by ancestor (not all the way to the <html> tag though), sibling and even a child. If you post the link to the that page I can have a look.
0

If your requirement to select specific textarea under a <td> then use the indexes because all the things seem common

<td colspan="60" width="60%" class="cell-right" valign="top">
  <textarea onkeydown="event.cancelBubble=true;" onkeypress="event.cancelBubble=true;" onkeyup="event.cancelBubble=true;" name="6c5c8088fb474f2b93e1de9fb1b1771d" id="6c5c8088fb474f2b93e1de9fb1b1771d" class="auto-size-field" rows="5"></textarea>
</td>
<td colspan="60" width="60%" class="cell-right" valign="top">
  <textarea onkeydown="event.cancelBubble=true;" onkeypress="event.cancelBubble=true;" onkeyup="event.cancelBubble=true;" name="6c5c8088fb474f2b93e1de9fb1b1771d" id="6c5c8088fb474f2b93e1de9fb1b1771d" class="auto-size-field" rows="5"></textarea>
</td>
<td colspan="60" width="60%" class="cell-right" valign="top">
  <textarea onkeydown="event.cancelBubble=true;" onkeypress="event.cancelBubble=true;" onkeyup="event.cancelBubble=true;" name="6c5c8088fb474f2b93e1de9fb1b1771d" id="6c5c8088fb474f2b93e1de9fb1b1771d" class="auto-size-field" rows="5"></textarea>
</td>

Suppose you want to enter some text under 2nd textarea then use below xpath :

//td[2]/textarea[@class='auto-size-field']

The other way can be locate the element based on unique parent element

e.g.

//div[@id='divid']/table/tr/td[2]/textarea[@class='auto-size-field']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.