简体   繁体   中英

How can find the Favorite button and click it with Selenium WebDriver?

Updated description:

Here is the listing from Redfin.com https://www.redfin.com/CA/Sunnyvale/735-Grape-Ave-94087/home/1835008 , what I want to locate and click is the "Favorite" button on the top-right corner. I've tried the code in the old description as well as all the suggestions by others, but none of them works.

Can someone guide me how to locate the icon in Selenium webdriver?

Thanks a lot!

+++++++++++++++++BELOW IS OLD PROBLEM DESCRIPTION+++++++++++++++++++++++++ I have this button:

<div role="button" title="Favorite" tabindex="0" class="clickable button tertiary-alt" data-rf-test-name="homeControlButton">
<span><svg class="SvgIcon rfSvg favorite svg-icon-off-color" style="height:24px;width:24px"><svg viewBox="0 0 24 24"></svg></svg></span>
</div>

But I tried with find XPath by:

browser.find_element_by_xpath("//*[@class='clickable button tertiary-alt' and @title='favorite']").click()

But it doesn't work. Any help?

Try

browser.find_elements_by_css_selector(".clickable.button.tertiary-alt");

Or you could do

browser.find_elements_by_css_selector("div[title=\"Favorite\"]");

You can use the following XPath:

//div[@title='Favorite']

Hope it helps you!

To click on the element you can target the child <span> tag and you can use either of the following Locator Strategies :

  • Using css_selector :

     browser.find_element_by_css_selector("div.clickable.button.tertiary-alt[title='Favorite']>span").click() 
  • Using xpath :

     browser.find_element_by_xpath("//div[@class='clickable button tertiary-alt' and @title='Favorite']/span").click() 

Here is the working code.

WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR , "div[data-rf-test-name='abp-favoriteButton'] div[role='button']")))
driver.find_element_by_css_selector("div[data-rf-test-name='abp-favoriteButton'] div[role='button']").click()

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