如何使用selenium单击图标

时间:2018-06-18 08:08:07

标签: python selenium webautomation

我正在尝试使用selenium自动执行某些活动。我正在启动一个网页,并出现一个安全窗口弹出窗口,用于登录凭据。

我使用Autoit进行自动化。现在登录后我需要点击该选项,我已经尝试了基于find_element_by_text和find_element_by_id。

我收到一个错误,因为无法找到带有CSS选择器的元素,我在StackOverflow中看到了一些其他帖子,但是我无法自己修复它。

以下是我的HTML的样子。能否请您指导我,请分享任何文件以便进一步检查。感谢。

    driver = webdriver.Ie(r"C:\\IEDriverServer\\IEDriverServer.exe")
    driver.get('URL of the page')
    #driver.implicitly_wait(10)  # seconds
    autoit.win_wait("Windows Security")
    # now make that crap active so we can do stuff to it
    autoit.win_activate("Windows Security")
    # type in the username
    autoit.send('username')
    # tab to the password field
    autoit.send("{TAB}")
    # type in the password
    autoit.send('password')
    # kill!
    autoit.send("{ENTER}")
    driver.maximize_window()
    driver.implicitly_wait(120)  # seconds
    #submit_button_incidents = driver.find_element_by_link_text("3-Normal Incidents")
    submit_button_incidents= driver.find_element_by_id("nodeImgs13pm")
    submit_button_incidents.click()
    driver.implicitly_wait(10)

++更新信息

我尝试复制整个HTML但页面受到限制,因此我无法查看基本模板以外的完整HTML页面。添加一些开发人员工具的截图。

enter image description here

这是我的网页的样子。

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试使用此代码

submit_button_incidents = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'My Group'])")))  
submit_button_incidents.click()  

并且不要在代码中使用隐式等待太多次。

为Web驱动程序实例的生命周期设置隐式等待。

参考:

Selenium official document for wiats
Xpath tutorial
cssSelector tutorial

更新:

由于OP已经共享了HTML代码。根据要求,您可以继续使用此代码。

因为有两个元素的文本作为我的组的队列。

对于第一个,您可以将XPATH写为:

//a[@class='scbdtext']/span[contains(text(),'My Group')]  

代码:

submit_button_incidents = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='scbdtext']/span[contains(text(),'My Group')]")))  
submit_button_incidents.click()

对于第二个,您可以将XPATH写为:

//a[@class='scbdtextfoc']/span[contains(text(),'My Group')]

代码:

submit_button_incidents = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[@class='scbdtextfoc']/span[contains(text(),'My Group')]")))  
submit_button_incidents.click()

希望这会有所帮助。

答案 1 :(得分:0)

双击使用ActionChains即可在Python中工作。

from selenium.webdriver import ActionChains
# Get the element however you want
element = driver.find_element_by_xpath("//a[@class='scbdtextfoc']/span[contains(text(),'My Group')]")
ActionChains(driver).double_click(settings_icon).perform()