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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21691126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:24:47  来源:igfitidea点击:

Find and click an item from 'onclick' partial value

javascriptpythonseleniumselenium-webdriverbeautifulsoup

提问by crookedleaf

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

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

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.

如果你注意到最后,有“一卧室豪华房”、“两卧室豪华房”和“一卧室总统房”。由于它是一个输入项目,因此没有任何我可以过滤的文本,但我只需要选择一个特定的项目,例如 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:

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

+--------------------------------------------------------------------+
|    1 Bedroom Deluxe    |      |   [button i don't care about]   |
|------------------------+---------+---------------------------------|
|    2 Bedroom Deluxe    |      |   [button i'm trying to click]  |
|------------------------+---------+---------------------------------|
| 1 Bedroom Presidential |      |   [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:

我猜这个帖子发布得太早了。紧接着,一位同事提出并建议通过 Xpath 查找元素:

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

采纳答案by Yi Zeng

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

XPath 或 CssSelector 都可以。不需要任何循环,但需要简单的定位器。

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

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

回答by GVH

You are on the right track!

你在正确的轨道上!

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

Yes, this exactly. You want to iterate through name = booksubmitelements and check the "onclick" attribute of each one. Here's what the rest would look like:

是的,正是这个。您想遍历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()

BeautifulSoup does not help much in this case, since you still need to use selenium's methods to get ahold of the element to click on.

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

回答by Anton

A general answer for solving these kinds of problems is using the Selenium IDE. From here, you can manually click buttons on a website and have the Selenium IDE (a Firefox plugin) record your actions. From there you can File > Export Test Case As... and choose whatever coding language you want.

解决这类问题的一般答案是使用 Selenium IDE。从这里,您可以手动单击网站上的按钮并让 Selenium IDE(一个 Firefox 插件)记录您的操作。从那里您可以文件 > 将测试用例导出为...并选择您想要的任何编码语言。

Selenium Downloads page: http://docs.seleniumhq.org/download/

Selenium 下载页面:http: //docs.seleniumhq.org/download/

Selenium IDE Firefox plugin version 2.9.0 http://release.seleniumhq.org/selenium-ide/2.9.0/selenium-ide-2.9.0.xpi

Selenium IDE Firefox 插件版本 2.9.0 http://release.seleniumhq.org/selenium-ide/2.9.0/selenium-ide-2.9.0.xpi

EDIT: Sometimes you might actually be finding the correct element, but the webdriver tries to find it before it's there. Try adding this line after you create your webdriver:
In Python:
driver.implicitly_wait(10)

This tells your webdriver to give itself more time to try to find the object. It does not add a 10 second wait, but says keep looking for 10 seconds. If it finds the element before 10 seconds, it will continue on as normal.

编辑:有时您实际上可能会找到正确的元素,但 webdriver 会尝试在它出现之前找到它。在创建 webdriver 后尝试添加以下行:
在 Python 中:
driver.implicitly_wait(10)

这告诉您的 webdriver 给自己更多的时间来尝试查找对象。它不会增加 10 秒的等待,而是说继续寻找 10 秒。如果它在 10 秒之前找到该元素,它将继续正常运行。