简体   繁体   English

Python Selenium无法通过类名或XPath找不到元素

[英]Python selenium unable to find element neither by class name nor xpath

I'm newbie in Selenium. 我是硒的新手。 I start to learn Selenium via book. 我开始通过书本学习硒。 And I struggle with unclear behavior of Selenium. 我为硒的不清楚行为而苦恼。 For educational purposes I use this site: http://magento-demo.lexiconn.com/ - I'm trying to find search button by its class name, (which is: class='button search button') or by it xpath 出于教育目的,我使用以下站点: http : //magento-demo.lexiconn.com/-我正尝试通过其类名(即class ='button search button')或xpath查找搜索按钮。

search_button = self.driver.find_element_by_xpath('/html/body/div/div[2]/header/div/div[4]/form/div[1]/button')

or 要么

search_button = self.driver.find_element_by_class_name('button')

but each time selenium unable to find it. 但是每次硒都找不到。 Please help me to understand reason of such behavior. 请帮助我了解这种行为的原因。 Thank you 谢谢

I used Selenium IDE and it shows me XPATH: //button[@type='submit'] 我使用了Selenium IDE,它向我展示了XPATH:// button [@ type ='submit']

when I tried to find element by xpath,I have got the same error and it is strange. 当我尝试通过xpath查找元素时,我遇到了相同的错误,这很奇怪。 Please advise. 请指教。

My code is: 我的代码是:

import unittest
from selenium import webdriver

class HomePageTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        #create new Firefox session
        cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        #navvigate to application home page
        cls.driver.get('http://magento-demo.lexiconn.com/')

    def test_search__text_field_max_length(self):
        #get the search text box
        search_field=self.driver.find_element_by_id("search")

        #check maxlenght attribute st to 128
        self.assertEqual("128",search_field.get_attribute("maxlength"))

    def test_search_button_enabled(self):
        # get Search button
        search_button = self.driver.find_element_by_class_name('button')

        # check Search button is enabled
        self.assertTrue(search_button.is_enabled())



    @classmethod
    def tearDown(self):
        #close the browser window
        self.driver.quit()


if __name__=='__main__':
    unittest.main(verbosity=2)

尝试这个 :

search_button = self.driver.find_element_by_xpath('//button[@class="button search-button"]')

Try downloading the selenium IDE plugin, install and start recording. 尝试下载Selenium IDE插件,安装并开始录制。 Click on the button you want and view how its target is recorded in the IDE. 单击所需的按钮,并查看其目标在IDE中的记录方式。 Programmatically, selenium will accept the same xpaths and other selectors as the IDE. 通过编程,selenium将接受与IDE相同的xpath和其他选择器。 After it's been recorded in the IDE, there is a pull down on the target field that lets you see all the different ways you can select that element, ie xpath vs. by class etc. 在IDE中将其记录后,将在目标字段上显示一个下拉列表,您可以通过该字段查看选择该元素的所有不同方式,例如xpath与按类等。

http://www.seleniumhq.org/projects/ide/ http://www.seleniumhq.org/projects/ide/

you might try: 您可以尝试:

css=button.button.search-button
//button[@type='submit']
//form[@id='search_mini_form']/div/button

I think the issue is that your locator isn't specific enough. 我认为问题在于您的定位器不够具体。 There is more than one button on the page and more than one element with class=button on the page. 页面上有多个button ,并且页面上有class = button多个元素。 This CSS selector is working for me. 这个CSS选择器为我工作。

self.driver.find_element_by_css_selector("button[title='Search']")

Try this way using xpath locator 使用xpath locator尝试这种方式

Explanation: Use title attribute of <button> tag. 说明:使用<button>标记的title属性。

self.driver.find_element_by_xpath("//button[@title='Search']")

OR 要么

Explanation: Use title and type attribute of <button> tag. 说明:使用<button>标签的titletype属性。

self.driver.find_element_by_xpath("//button[@title='Search'][@type='submit']")

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

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