1

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?

damingzi
  • 676
  • 10
  • 28
  • 2
    The button title is `Favorite` not `favorite` – Danielle M. Mar 22 '19 at 20:53
  • What Danielle said... and you probably can make that locator simpler. I'm assuming that using the `title` is enough to uniquely locate the element on the page, e.g. `//div[@title='Favorite']"`. – JeffC Mar 22 '19 at 21:30
  • @DanielleM. Hi Daniel, I've updated the problem description, can you please let me know if you have any ideas? Thanks! – damingzi Mar 28 '19 at 05:19

4 Answers4

1

Try

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

Or you could do

browser.find_elements_by_css_selector("div[title=\"Favorite\"]");
Asyranok
  • 950
  • 1
  • 7
  • 17
  • I've updated the problem description, can you please let me know if you have any ideas? Thanks! – damingzi Mar 28 '19 at 05:19
  • `$('[title="Favorite"]')` I ran that in the console and it found it. This div is not nested in any frames, so the same selector will work with selenium. `browser.find_element_by_css_selector("[title='Favorite']");` If that doesn't work, there is definitely something wrong with your selenium driver, and you may want to update it, or downgrade it. – Asyranok Mar 28 '19 at 14:23
  • thanks, but I still cannot find the Favorite, please see the following log. `browser.find_element_by_css_selector("[title='Favorite']").click() selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [title='Favorite']` – damingzi Mar 28 '19 at 17:43
  • Thanks Asyranok! I found the root cause. The reason why Favorite cannot be found is because the page isn't fully loaded. – damingzi Mar 28 '19 at 17:57
1

You can use the following XPath:

//div[@title='Favorite']

Hope it helps you!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • 1
    Message: Unable to locate element: //div[@title='Favorite'] It looks like it still cannot be able to find the button :( – damingzi Mar 22 '19 at 22:10
  • @damingzi, seems your element inside the `iframe`. So, first, switch to it before interaction with the element. – Ratmir Asanov Mar 23 '19 at 08:56
1

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()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Check if there are any frames or window. `print(len(driver.window_handles))` and `print(len(driver.find_elements_by_tag_name('iframe')))`. – supputuri Mar 22 '19 at 23:20
  • @supputuri (len(driver.window_handles)) == 1 and len(driver.find_elements_by_tag_name('iframe')) == 6, does it say anything? Thanks! – damingzi Mar 23 '19 at 03:17
  • @damingzi Yes, it does. Your element is inside `iframe`. In order to find the element you have to switch the context to look for elements inside the `iframe` – Fenio Mar 23 '19 at 09:13
  • 1
    Ah, that's great. So here is a learning opportunity for both of us. Make sure you add as much of your code as possible. If I had seen the lines of code before this driver.find logic, I would have instantly seen that you weren't waiting for the page to fully load before searching for this element. And I will also make sure that I request that extra info next time as well. – Asyranok Mar 28 '19 at 18:08
  • 1
    4 contributors and 7 days of intercommunication to solve a question counts as a bit expensive but glad to know OP got a working solution. The key takeaways would be 1)To provide as much as possible _back ground context_ 2) _Expected output_ 3) Relevant _HTML_ or _link_ to the site 4) _Code trials_ and 5) _Error stack trace_. In presence of these 5 parameters nailing the bug/defect/issue becomes much easier. – undetected Selenium Mar 28 '19 at 18:51
1

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()
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Thanks you. But the Favorite isn't wrapped inside a iframe, it's in div. I've updated the problem description, can you please let me know if you have any ideas? Thanks! – damingzi Mar 28 '19 at 05:19
  • Updated the answer with the tested code. It's working for me. Let me know if you still have issue. – supputuri Mar 28 '19 at 13:49
  • I've tried again, but still it cannot find it :( `selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: div[data-rf-test-name='abp-favoriteButton'] div[role='button']` – damingzi Mar 28 '19 at 17:48
  • Thank you Supputuri. I found the root cause. I have to wait until the page is fully loaded. After I explicitly wait for few seconds, the problem solved!!! – damingzi Mar 28 '19 at 17:58