Unable to locate element using selenium webdriver in python

做~自己de王妃 提交于 2020-11-29 08:32:22

问题


I want to do some automation testing on a website called http://elegalix.allahabadhighcourt.in. I am using the following python code to click a button called "Advanced" on the above website:

Code#

from selenium import webdriver
driver = webdriver.Chrome('./chromedriver')
driver.get('http://elegalix.allahabadhighcourt.in')
driver.set_page_load_timeout(20)
driver.maximize_window()
driver.find_element_by_xpath("//input[@value='Advanced']").click()

Error#

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@value='Advanced']

P.S I am a newbie to python programming. I have tried different variations of xpath and have tried to use other find_element_by methods but none seems to work on this website...I m experiencing similar error on Firefox browser as well...


回答1:


It's because the element you are looking for is inside a frame, switch to the frame first and then search for the element

from selenium import webdriver
driver = webdriver.Chrome('./chromedriver')
driver.get('http://elegalix.allahabadhighcourt.in')
driver.set_page_load_timeout(20)
driver.maximize_window()
driver.switch_to.frame(driver.find_element_by_name('sidebarmenu'))
driver.find_element_by_xpath("//input[@value='Advanced']").click()
driver.switch_to.default_content()



回答2:


I faced with this issue and the problem about the window size. Because when windows size is small objects place has changed so i use driver.maximize_window() and then i use driver.find method it works properly.



来源:https://stackoverflow.com/questions/44661187/unable-to-locate-element-using-selenium-webdriver-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!