簡體   English   中英

使用 Selenium/Python 查找按鈕元素

[英]Find button element using Selenium/Python

我正在嘗試在下一頁上找到一個按鈕 - https://www.dukascopy.com/swiss/english/marketwatch/sentiment/

我試圖找到的按鈕是流動性提供者按鈕

我嘗試在 driver.find_element_by_xpath(...) 中同時使用 xpath 和完整的 xpath ,但它似乎根本找不到按鈕。 我也嘗試了所有其他 find_element 方法,但無濟於事。

使用 Xpath 時出現以下錯誤

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id=":1"]"} (Session info: chrome=83.0.4103.97)

任何幫助,將不勝感激。 謝謝

首先,它根本不是一個按鈕。 這是一個div。 其次,它在 iframe 內部。 iframe 或框架內的元素不存在於主頂級文檔中。 您必須切換才能看到它們。 像這樣的東西會起作用。

# get the iframe element
iframe = driver.get_element_by_tag_name('iframe')
# switch to it
driver.switch_to_frame(iframe)
# with the new frame active locate the element
liquidity_customers = driver.find_element_by_xpath("//*[text()='Liquidity consumers']").click()
# switch back to top level of document
driver.switch_to_default_content()

查看頁面唯一使用“流動性提供者”的地方是在您想要的區域,所以,

driver.find_elements_by_xpath("//*[contains(text(), 'Liquidity providers')]")

注意:這將返回一個元素列表,您需要列表中的唯一一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM