如何在 selenium webdriver python 中单击以下元素

How to click the below element in selenium webdriver python

我想在 python 中使用 selenium webdriver 单击下面的“登录”选项。有人可以帮我解决这个问题吗?

<div class="item-4TFSfyGO item-ykcJIrqq item-50IqnBef withIcon-4TFSfyGO withIcon-ykcJIrqq" data-name="header-user-menu-sign-in"><div class="icon-4TFSfyGO"><svg height="28" viewbox="0 0 28 28" width="28" xmlns="http://www.w3.org/2000/svg"><path d="M17.5 9c0 1.14-.3 1.99-.79 2.54-.46.52-1.27.96-2.71.96s-2.25-.44-2.71-.96A3.74 3.74 0 0 1 10.5 9c0-1.14.3-1.99.79-2.54.46-.52 1.27-.96 2.71-.96s2.25.44 2.71.96c.5.55.79 1.4.79 2.54zM19 9c0 2.76-1.45 5-5 5s-5-2.24-5-5 1.45-5 5-5 5 2.24 5 5zm-8 8.5h6c2.04 0 3.1.5 3.76 1.1.69.63 1.11 1.55 1.5 2.8.13.42.04.95-.29 1.4-.33.46-.8.7-1.22.7H7.25c-.43 0-.89-.24-1.22-.7a1.61 1.61 0 0 1-.3-1.4 6.08 6.08 0 0 1 1.51-2.8c.65-.6 1.72-1.1 3.76-1.1zm6-1.5h-6c-4.6 0-5.88 2.33-6.7 4.96-.58 1.89.97 4.04 2.95 4.04h13.5c1.98 0 3.53-2.15 2.95-4.04C22.88 18.33 21.6 16 17 16z" fill="currentColor" fill-rule="evenodd"></path></svg></div><div class="labelRow-4TFSfyGO labelRow-ykcJIrqq"><div class="label-4TFSfyGO">Sign in</div></div></div>

编辑: 我通常会找到 XPATH 和 driver.find_element(By.XPATH, button).click() 来点击 buttons/hyperlinks。 在此页面中,加载后,我可以使用 driver.find_element(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[3]/button[1]").click() 单击“用户”图标,然后显示带有“登录”选项的菜单。我无法使用“检查”选项在 firefox 中找到 XPATH。 然后我保存了 html 页面并检查了“登录”选项的定义,并没有在其中找到任何 button/hyperlink 定义。所以,我想在这方面得到一些帮助,以便进一步进行。谢谢

您可以使用下面的 CSS_SELECTORexplicit waits:

点击参与按钮

代码:

driver.maximize_window()
wait = WebDriverWait(driver, 30)


driver.get("https://in.tradingview.com/")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Open user menu']"))).click()

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@data-name='header-user-menu-sign-in' and contains(.,'Sign in')]"))).click()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

截图: