usermt
usermt

Reputation: 31

Selenium/Python - Class name with spaces unable to local element

I'm having trouble locating an element. I'm trying to locate it and enter some data into the field. I notice that the class name has spaces and ID is generated automatically (compare these to other forms) so can't use the ID for the automation as I want to automate this to create new forms and will be using the 'Description' field every time.

Below is the html for the Description field, which I'm trying to locate.

<input size="15" maxlength="255" class="acitem description s-description ui-autocomplete-input" spinner="/assets/spinner-48c6e73f2bbe9ea753f7f8e5410541a8138d19d657ddd532b2765335ed3d62bf.gif" auto_complete="true" data-autocomplete-url="/items/auto_complete" data-autocomplete-renderer="item_autocomplete_renderer" data-autocomplete-delay="250" type="text" name="invoice[invoice_lines_attributes][68345][description]" id="invoice_invoice_lines_attributes_68345_description" autocomplete="off">

The codes that I'm using so far has failed.

test_1 = driver.find_element_by_css_selector('.acitem.description.s-description.ui-autocomplete-input')
test_1.send_keys("HELLO WORLD")

test_2 = driver.find_element_by_css_selector("input[class='acitem description s-description ui-autocomplete-input']")
test_2.send_keys("HELLO WORLD")

test_3 = Select(driver.find_element_by_xpath("//*[@class='acitem description s-description ui-autocomplete-input']"))
test_3.send_keys("HELLO WORLD")

Did I got the code wrong or is there some workaround with the class name that has spaces? Thanks.

Upvotes: 0

Views: 906

Answers (3)

usermt
usermt

Reputation: 31

Thanks for all your help. I solved by using start-with and contains. Below is my code.

invc_desc =driver.find_element_by_xpath("//input[starts-with(@class,'acitem') and contains(@class,'s-description')]")
invc_desc.clear()
invc_desc.send_keys("HELLO WORLD")

Upvotes: 1

Alexandru Sichim
Alexandru Sichim

Reputation: 1

I almost all the cases, all the elements from the DOM can be accessed via XPATH. In your case I would go with the following:

element = driver.find_element_by_xpath("//input[@id='acitem description s-description ui-autocomplete-input']")

Upvotes: 0

Infern0
Infern0

Reputation: 2814

Try with xpath

//input[starts-with(@id,'invoice_invoice_lines_attributes_')]

Upvotes: 0

Related Questions