繁体   English   中英

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

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

更新的说明:

这是Redfin.com上的列表https://www.redfin.com/CA/Sunnyvale/735-Grape-Ave-94087/home/1835008 ,我要查找并单击的是顶部的“收藏夹”按钮-右上角。 我已经尝试了旧描述中的代码以及其他人的所有建议,但是都没有用。

有人可以指导我如何在Selenium Webdriver中找到图标吗?

非常感谢!

+++++++++++++++++++以下是旧问题说明+++++++++++++++++++++++++我有这个按钮:

<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>

但是我尝试通过以下方式查找XPath:

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

但这是行不通的。 有什么帮助吗?

尝试

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

或者你可以做

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

您可以使用以下XPath:

//div[@title='Favorite']

希望对您有帮助!

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

  • 使用css_selector

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

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

这是工作代码。

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