查找并从“的onclick”部分值单击项目 [英] Find and click an item from 'onclick' partial value

查看:1444
本文介绍了查找并从“的onclick”部分值单击项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过的onclick 元素的部分值硒通过点击一个元素?

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

例子是:

 <输入名称=booksubmit类型=按钮级=searchAvailBtnSelect值=选择 onclick=\"setTimeout('disableSelect()',1);bookNowSubmit('0165','1BD','000000452014022703S000016500010708F ','101400','156000','3','02/27/2014','false','false','false','false','true','false','false','EX$p$pSS','63','1卧室豪华','假','交流')>
<输入名称=booksubmit类型=按钮级=searchAvailBtnSelect值=选择 onclick=\"setTimeout('disableSelect()',1);bookNowSubmit('0165','2BD','000000452014022703S000016500010708F ','101400','156000','3','02/27/2014','false','false','false','false','true','false','false','EX$p$pSS','63','2卧室豪华','假','交流')>
<输入名称=booksubmit类型=按钮级=searchAvailBtnSelect值=选择 onclick=\"setTimeout('disableSelect()',1);bookNowSubmit('0165','1BD','000000452014022703S000016500010708F ','101400','156000','3','02/27/2014','false','false','false','false','true','false','false','EX$p$pSS','63','1卧室presidential','假','交流')>

如果你接近尾声注意到,有一个1卧室豪华,2卧室豪华和1卧室presidential。因为它是一个输入项,没有,我将能够通过过滤任何文字,但我只需要选择一个特定的项目,如2卧室豪华。

有什么我可以的意思做的:

 按钮= driver.find_elements_by_name('booksubmit')
在按键按钮:
    如果按钮........

东西或其他?我目前使用beautifulsoup4也解析页面上的HTML和检索与该项目相关的文本,所以我不知道这是否可以被导入。从外观上看,该页面是一个HTML表中的格式为:

  + ------------------------------- ------------------------------- +
| 1卧室豪华| $ 25 | [按钮我不在乎] |
| ------------------------ + --------- + -------------- ------------------- |
| 2卧室豪华| $ 50 | [按钮我试图点击] |
| ------------------------ + --------- + -------------- ------------------- |
| 1卧室presidential | $ 50 | [按钮我不在乎] |
+ ------------------------------------------------- ------------------- +

编辑:

我猜得很快公布这一点。紧接着,一个coworked上来,并建议通过XPath和发现的元素:

  driver.find_element_by_xpath('//输入[包含(@onclick,1卧室豪华)]')


解决方案

XPath或CssSelector会怎么做。不需要有任何的循环,但简单的定位器。

  driver.find_element_by_xpath(.//输入[包含(@onclick,'1卧室豪华')])driver.find_element_by_css_selector(输入[*的onclick =1卧室豪华'])

Is it possible to click an element through selenium by a partial value of an onclick element?

There are multiple input items on a page, and I only need to select one with a specific string.

Examples would be:

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

If you notice towards the end, there is a "1 Bedroom Deluxe", "2 Bedroom Deluxe", and "1 Bedroom Presidential". Since it is an input item, there isn't any text that I would be able to filter by, but I need to only select a specific item, such as the 2 Bedroom Deluxe.

Is there anything I could do in the sense of:

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

something or another? I'm currently using beautifulsoup4 to also parse the html on the page and retrieve text that is associated with the item, so i don't know if that could be incorporated at all. Visually, the page is an HTML table that is in the format of:

+--------------------------------------------------------------------+
|    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]   |
+--------------------------------------------------------------------+

EDIT:

I guess posted this too soon. Right after, a coworked came up and suggested finding the element by Xpath with:

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

解决方案

Either XPath or CssSelector would do. No need to have any looping, but straightforward locators.

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

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

这篇关于查找并从“的onclick”部分值单击项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆