简体   繁体   English

如何找到“收藏夹”按钮并使用Selenium WebDriver单击它?

[英]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. 这是Redfin.com上的列表https://www.redfin.com/CA/Sunnyvale/735-Grape-Ave-94087/home/1835008 ,我要查找并单击的是顶部的“收藏夹”按钮-右上角。 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? 有人可以指导我如何在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: 但是我尝试通过以下方式查找XPath:

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: 您可以使用以下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 : 要单击该元素,您可以定位子<span>标签,并且可以使用以下两种定位策略之一

  • Using css_selector : 使用css_selector

     browser.find_element_by_css_selector("div.clickable.button.tertiary-alt[title='Favorite']>span").click() 
  • Using xpath : 使用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()

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

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