2

so basically I'm doing web scraping/website automation,

Here's a simple example of what I'm trying to do:

I'm working on a dynamic page that contains a form, and I want to insert data in this form.

Naturally, this dynamic page changes depending on what I inserted before happening on this page. Here's an example of the input field ID of 5 different pages which I'm trying to insert data in. It is therefore generated automatically, and unique:

  1. Listing.Item[Chess Size]
  2. Listing.Item[Shoe Size]
  3. Listing.Item[Size Trouser]
  4. Listing.Item[Size]
  5. Listing.Item[Shoe Size]

The same pattern is the string 'Size' here.

I need to insert data in this field, otherwise, I can't move forward on the form. So here's what I tried:

driver.find_element_by_id('Listing.Item[' + any(str) + 'Size' + any(str) + ']')

But it doesn't work. Maybe I'm wrongly using the function 'any()'.

How can automatically find this unique input in every page I come across, which always contains the string 'Size' in its ID ?

XPath & CSS Selector also contains the other word (for this example: Chess, Shoe, and Trouser) in consequence I can't use them

THANKS for help

EDIT:

Here's the whole corresponding HTML:

 <input type="text" id="Listing.Item[Shoe Size]"
 name="_st_Shoe Size" size="21" maxlength="50"
 aria-required="true" value="">

=> the value of name and id change each time

Michael
  • 503
  • 8
  • 18

1 Answers1

3

What you probably want to try is the find_elements_by_css_selector. This function accepts (as the name implies) css selectors to return elements.

The CSS selector allows you to search for ids that contain certain values.

For example, if you were searching for all input tags that had id's containing the word "Size", you'd use:

css_selector = 'input[id*=Size]'
driver.find_elements_by_css_selector(css_selector)

The *= means "contains".

Here are some resources you may find helpful:

Selenium CSS Selectors

Another question similar to this

Nice compilation of CSS selectors

Community
  • 1
  • 1