简体   繁体   English

如何找到并单击带有硒的隐藏按钮?

[英]How to find and click on a hidden button with selenium?

I am trying to use a selenium python script to try to click on a button which is only visible when the mouse hovers over a certain region of the webpage.我正在尝试使用 selenium python 脚本来尝试单击仅当鼠标悬停在网页的某个区域上时才可见的按钮。 The code I use so far is as follows:我目前使用的代码如下:

driver = webdriver.Firefox()
driver.implicitly_wait(20)
driver.get("https://www.swisscom.ch/de/privatkunden/mobile/devices.html")
element = driver.find_element_by_xpath("//div[@id='gridProduct10247118']")
hrefs = element.find_elements_by_class_name('primary-button')
print(len(hrefs))

which seems to work, but returns 0 entries in hrefs .这似乎有效,但在hrefs中返回0个条目。 But in the inspector of firefox I clearly can see this element within the element of the given id :但是在 firefox 的检查器中,我清楚地可以在给定id的元素中看到这个元素:

在此处输入图像描述

What am I doing wrong?我究竟做错了什么? How to find that button element and be able to click it?如何找到该按钮元素并能够单击它?

PS I am not able to use the record ability of selenium ( see here ) because the button becomes only visible when the mouse is hovered above a certain element, as shown here: PS 我无法使用 selenium 的记录功能( 请参阅此处),因为该按钮仅在鼠标悬停在某个元素上方时才可见,如下所示:

在此处输入图像描述

Addendum:附录:

Given the 'help' I change a line in the script to:鉴于“帮助”,我将脚本中的一行更改为:

hrefs = element.hover().find_elements_by_class_name('primary-button').click()

which gives an error:这给出了一个错误:

AttributeError: 'WebElement' object has no attribute 'hover'

Addendum on the help by Andersson: I change part of the code as follows:安德森帮助的附录:我将部分代码更改如下:

element = driver.find_element_by_xpath("//div[@id='gridProduct10247118']")
hover = ActionChains(driver).move_to_element(element)
hover.perform()
hrefs = hover.find_elements_by_xpath("//a[@class='primary-button']")

but got an error但出现错误

AttributeError: 'ActionChains' object has no attribute 'find_elements_by_xpath'

If I use the following line of code instead:如果我改用以下代码行:

hrefs = element.find_elements_by_xpath("//a[@class='primary-button']")

I get 9 elements, although I expecte only one single "Bestellen"-button element (see images in the question).我得到了 9 个元素,尽管我希望只有一个“Bestellen”按钮元素(参见问题中的图像)。

I tried with two different approaches我尝试了两种不同的方法

driver = webdriver.Firefox()
print driver
driver.implicitly_wait(20)
driver.get("https://www.swisscom.ch/de/privatkunden/mobile/devices.html")

Directly finding elements with class_name = 'primary-button'使用 class_name = 'primary-button' 直接查找元素

hrefs = driver.find_elements_by_class_name('primary-button')
print hrefs
print(len(hrefs))

Result:结果:

[<selenium.webdriver.remote.webelement.WebElement object at 0xe390d0>, <selenium.webdriver.remote.webelement.WebElement object at 0xe39290>, <selenium.webdriver.remote.webelement.WebElement object at 0xe39250>, <selenium.webdriver.remote.webelement.WebElement object at 0xe391d0>, <selenium.webdriver.remote.webelement.WebElement object at 0xe394d0>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ed10>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ecd0>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ec50>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ec10>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ec90>]
10

On div element find all buttons using xpath在 div 元素上使用 xpath 查找所有按钮

element = driver.find_element_by_xpath("//div[@id='gridProduct10247118']")
hrefs = element.find_elements_by_xpath("//a[@class='primary-button']")
print hrefs
print len(hrefs)

Result:结果:

[<selenium.webdriver.remote.webelement.WebElement object at 0xe4ee10>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ee50>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ee90>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4eed0>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ef10>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ef50>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4ef90>, <selenium.webdriver.remote.webelement.WebElement object at 0xe4efd0>, <selenium.webdriver.remote.webelement.WebElement object at 0xe58050>]
9

And your approach:你的方法:

element = driver.find_element_by_xpath("//div[@id='gridProduct10247118']")
hrefs = element.find_elements_by_class_name('primary-button')
print hrefs
print len(hrefs)

Result:结果:

[]
0

On the other hand finding elements by xpath can be relative to other elements .另一方面,通过 xpath 查找元素可以相对于其他元素。 From the docs:从文档:

You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute.您可以使用 XPath 以绝对术语(不建议)或相对于具有 id 或 name 属性的元素来定位元素。

So the second approach gives you list of required elements因此,第二种方法为您提供了所需元素的列表

Update :更新

The real problem is that the target element is not visible until you hover on the parent div.真正的问题是,直到您将鼠标悬停在父 div 上,目标元素才可见。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()
driver.get("https://www.swisscom.ch/de/privatkunden/mobile/devices.html")

div_element = WebDriverWait(driver, 60).until(expected_conditions.presence_of_element_located((By.ID, 'gridProduct10247118')))
hover = ActionChains(driver).move_to_element(div_element)
hover.perform()

button = WebDriverWait(driver, 30).until(expected_conditions.presence_of_element_located((By.XPATH, "//div[@id='gridProduct10247118']//a[@class='primary-button']")))
hover = ActionChains(driver).move_to_element(button)
hover.perform()

button.click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM