简体   繁体   中英

Selenium find element and click on it

I'm trying to get Selenium to click on View All Companies button, but i'm not sure what am I doing wrong. It returns no element found

html code

<div class="screener-toggles">
  <div class="buttons">
    <span class="button selected" data-name="advanced-screener">Search by Screener<span data-name="advanced-screener" class="arrow selected"></span></span>
    <span class="button" data-name="alpha-factors">Search by Alpha Factors<span data-name="alpha-factors" class="arrow"></span></span>
    <span class="button" data-name="all-companies">View All Companies<span data-name="all-companies" class="arrow"></span></span>
  </div>
</div>

python code I wrote

element1 = driver.find_elements_by_class_name('View All Companies')
element1.click()
# I have tried all-companies instead of View All Companies as well. But still doesn't work

Should I not be using find_elements_by_class_name ?

Any advice on what I am doing wrong is greatly appreciated!

试试xpath"//span[contains(text(),'View All Companies')]"

Yes, you should not use the find_elements_by_class_name instead of use find_element_by_class_name .

find_elements_by_class_name is used when your expecting your locator to return more than 1 element. for a specific element use only find_element_by_class_name .

Another thing is I am not able to see any class name as View All Companies in your HTML code. Please look into your HTML and select classname or other locator carefully

Hope it will help you

View All Companies is text, not the class. Try looking by text with css_selector or xpath

element1 = find_element_by_css_selector('span:contains("View All Companies")')

element1 = find_element_by_xpath('//span[contains(text(), "View All Companies")]')

Or by the data-name attribute which contains all-companies

element1 = find_element_by_css_selector('span[data-name*="all-companies"]')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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