一尘不染

从“ onclick”部分值中查找并单击项目

selenium

是否可以通过元素的部分值单击selenium中的onclick元素?

页面上有多个输入项目,我只需要选择一个带有特定字符串的项目即可。

例如:

<input name="booksubmit" type="button" class="searchAvailBtnSelect" value="Select" onclick="setTimeout('disableSelect()',1);bookNowSubmit('0165','1BD','000000452014022703S000016500010708F ','101400','156000','3','02/27/2014','false','false','false','false','true','false','false','EXPRESS','63','1 Bedroom Deluxe','false','AC')">
<input name="booksubmit" type="button" class="searchAvailBtnSelect" value="Select" onclick="setTimeout('disableSelect()',1);bookNowSubmit('0165','2BD','000000452014022703S000016500010708F ','101400','156000','3','02/27/2014','false','false','false','false','true','false','false','EXPRESS','63','2 Bedroom Deluxe','false','AC')">
<input name="booksubmit" type="button" class="searchAvailBtnSelect" value="Select" onclick="setTimeout('disableSelect()',1);bookNowSubmit('0165','1BD','000000452014022703S000016500010708F ','101400','156000','3','02/27/2014','false','false','false','false','true','false','false','EXPRESS','63','1 Bedroom Presidential','false','AC')">

如果您注意到快要结束了,这里有“ 1卧室豪华房”,“ 2卧室豪华房”和“
1卧室总统房”。由于它是一个输入项目,因此没有可供我过滤的文本,但是我只需要选择一个特定的项目,例如2 Bedroom Deluxe。

我有什么可以做的:

buttons = driver.find_elements_by_name('booksubmit')
for button in buttons:
    if button ........

还是其他?我目前正在使用beautifulsoup4来解析页面上的html并检索与该项目关联的文本,因此我不知道是否可以将其合并。从视觉上看,该页面是HTML表格,其格式为:

+--------------------------------------------------------------------+
|    1 Bedroom Deluxe    |   $25   |   [button i don't care about]   |
|------------------------+---------+---------------------------------|
|    2 Bedroom Deluxe    |   $50   |   [button i'm trying to click]  |
|------------------------+---------+---------------------------------|
| 1 Bedroom Presidential |   $50   |   [button i don't care about]   |
+--------------------------------------------------------------------+

编辑:

我猜发布得太早了。紧接着,一个同事提出并建议通过Xpath通过以下方式找到元素:

driver.find_element_by_xpath('//input[contains(@onclick,"1 Bedroom Deluxe")]')

阅读 227

收藏
2020-06-26

共1个答案

一尘不染

XPath或CssSelector都可以。无需任何循环,但定位器简单明了。

driver.find_element_by_xpath(".//input[contains(@onclick, '1 Bedroom Deluxe')]")

driver.find_element_by_css_selector("input[onclick*='1 Bedroom Deluxe']")
2020-06-26