【问题标题】:How to navigate a website ,hover mouse over sub menu and click on a website using selenium python如何导航网站,将鼠标悬停在子菜单上并使用 selenium python 单击网站
【发布时间】:2018-05-01 05:53:19
【问题描述】:

我试图通过使用 selenium python 来自动化我的一个 Web 应用程序“maximo”。 我可以使用用户名和密码登录,也可以使用“find_element_by_”等单击任何单个按钮。但我不知道如何将鼠标悬停在子菜单上并单击。

我还试图找到一种方法来调用与菜单对应的 Javascript 代码,这些代码显示在这个 maximo 的底部(附图片)。 enter image description here

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

browserchr3=webdriver.Chrome('C:\chromedriver\chromedriver.exe')
browserchr3.get('http://da-maximo.dca.com/maximo/webclient/login/login.jsp?welcome=true')
browserchr3.find_element_by_id('username').send_keys('bhsek015'+Keys.TAB)

userpassword=browserchr3.find_element_by_id('password')
userpassword
userpassword.send_keys('Adil@2020')
userpassword.submit()
#browserchr3.execute_script(topLevelMenus['m7f8f3e49_ns_mc'].menuClick({"id":"WOTRACK_APP","text":"Work Order Tracking","eventvalue":"WOTRACK","apptype":"RUN","target":"startcntr","event":"changeapp","value":"WOTRACK"}))
browserchr3.implicitly_wait(10)                      

Select(browserchr3.find_element_by_css_selector('#m7f8f3e49_ns_menu_WO_MODULE_a')).select_by_visible_text("Work Order Tracking")
wo
wo.click()
browserchr3.implicitly_wait(10)
wot=browserchr3.find_element_by_css_selector('#menuholdertd')
wot
wot.click()

【问题讨论】:

    标签: javascript python-3.x selenium


    【解决方案1】:

    对于您的问题1,将鼠标悬停在元素上,您需要使用动作链

    from selenium.webdriver.common.action_chains import ActionChains
    
    actions = ActionChains(browserchr3)
    # Assuming wot is the element to move your mouse to
    actions.move_to_element(wot)
    actions.perform()    #Performs chained action
    

    您可以像这样链接操作 ActionChains(browserchr3).move_to_element(wot).perform()

    如需完整参考,请查看 API => https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html

    对于问题2,如何执行javascript,

    browserchr3.execute_script('Script statement')
    

    【讨论】:

    • 谢谢,虽然你的代码没有给出任何错误,但是它打开了主菜单,但没有进入子菜单并点击,只是闪烁主菜单,什么都不做。
    • 您需要进一步链接它。如果“菜单”是您将鼠标悬停的元素,而“子菜单”是您实际单击的元素,请尝试执行类似操作。 ActionChains(browserchr3).move_to_element(menu).pause(1).move_to_element(sub_menu).click().perform() 这也可以拆分成多个语句...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2015-10-16
    • 1970-01-01
    相关资源
    最近更新 更多