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