带有空格的Selenium/Python类名无法本地elemen

2024-05-14 18:58:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我找不到一个元素。我正试图找到它并在字段中输入一些数据。我注意到类名有空格,并且ID是自动生成的(与其他表单比较),所以不能使用ID进行自动化,因为我希望自动创建新表单,并且每次都使用“Description”字段。在

下面是描述字段的html,我正试图找到它。在

<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">

到目前为止我使用的代码都失败了。在

^{pr2}$

我是不是把代码搞错了,或者是有一些解决方法来解决类名中有空格的问题?谢谢。在


Tags: id表单autoinputdatainvoicedescriptionautocomplete
3条回答

几乎所有情况下,DOM中的所有元素都可以通过XPATH访问。 在你的情况下,我会同意以下几点:

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

尝试使用xpath

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

谢谢你的帮助。我用start-with和contains解决了这个问题。 下面是我的代码。在

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")

相关问题 更多 >

    热门问题