查找并单击"onclick"部分值中的项目

cro*_*eaf 7 javascript python selenium beautifulsoup selenium-webdriver

是否可以通过元素的部分值通过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')">
Run Code Online (Sandbox Code Playgroud)

如果您注意到最后,有"一卧室豪华","两卧室豪华"和"一卧室总统".由于它是一个输入项,因此我没有任何文本可以过滤,但我只需要选择一个特定的项目,例如2卧室豪华版.

在以下意义上我能做些什么:

buttons = driver.find_elements_by_name('booksubmit')
for button in buttons:
    if button ........
Run Code Online (Sandbox Code Playgroud)

什么或另一个?我目前正在使用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]   |
+--------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

编辑:

我想这个太快了.之后,一个接过来的人建议通过Xpath找到元素:

driver.find_element_by_xpath('//input[contains(@onclick,"1 Bedroom Deluxe")]')
Run Code Online (Sandbox Code Playgroud)

Yi *_*eng 9

XPath或CssSelector都可以.不需要任何循环,但直接定位器.

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

driver.find_element_by_css_selector("input[onclick*='1 Bedroom Deluxe']")
Run Code Online (Sandbox Code Playgroud)


GVH*_*GVH 7

你走在正确的轨道上!

buttons = driver.find_elements_by_name('booksubmit')
for button in buttons:
Run Code Online (Sandbox Code Playgroud)

是的,这完全是.您想迭代name = booksubmit元素并检查每个元素的"onclick"属性.这是其余的样子:

buttons = driver.find_elements_by_name('booksubmit')
for button in buttons:
    onclick_text = button.get_attribute('onclick')
    if onclick_text and re.search('Bedroom Deluxe', onclick_text):
        print "found it!"
        button.click()
Run Code Online (Sandbox Code Playgroud)

在这种情况下,BeautifulSoup没有多大帮助,因为您仍然需要使用selenium的方法来获取要点击的元素.