简体   繁体   中英

How to find href through class name and click with selenium

I'm using Selenium with python to make a spider.

A part of the web page is like this:

<a href="http://www.example.html" target="_blank" class="_j_search_link">text - <span class="sr-keyword">name</span> text</a>

I need to find the href and click.

I've tried as below:

target = driver.find_element_by_class_name('_j_search_link')
target.click()

target is not None but it doesn't seem that it could be clicked because after target.click() , nothing happened.

Also, I've read this question: Click on hyperlink using Selenium Webdriver
But it can't help me because in my case, there is a <span class> , not just a simple text Google .

您可以使用_j_search_link_j_search_link包含文本Wat Chedi Luang的元素

driver.find_element_by_xpath('//a[@class="_j_search_link" and contains(., "Wat Chedi Luang")]')
driver.find_elements_by_partial_link_text("Wat Chedi Luang").click()

I think you didn't find right element. Use CSS or Xpath to target this element then click it, like:

//a[@class='_j_search_link']

or

//a[@class='_j_search_link']/span[@class='sr-keyword']

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