简体   繁体   English

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

[英]How to click the below element in selenium webdriver python

I would like to click on the below "sign in" option using selenium webdriver in python. Can someone help me on this?我想在 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>

EDIT: I usually find the XPATH and driver.find_element(By.XPATH, button).click() to click on buttons/hyperlinks.编辑:我通常会找到 XPATH 和driver.find_element(By.XPATH, button).click()点击按钮/超链接。 In this page, after loading it, I was able to click on the "user" icon using driver.find_element(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[3]/button[1]").click() after which it shows menu with "sign in" option.在此页面中,加载后,我可以使用driver.find_element(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[3]/button[1]").click()单击“用户”图标driver.find_element(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[3]/button[1]").click()之后显示带有“登录”选项的菜单。 I couldnt find the XPATH for that in firefox using "Inspect" option.我无法使用“检查”选项在 firefox 中找到 XPATH。 Then I saved the html page and checked the definition of "sign in" option and didnt find any button/hyperlink definition in it.然后我保存了 html 页面并检查了“登录”选项的定义,并没有在其中找到任何按钮/超链接定义。 So, I would like some help on this to proceed further.所以,我想在这方面得到一些帮助,以便进一步进行。 Thanks谢谢

You can click on sing in button using the below CSS_SELECTOR with explicit waits :您可以使用以下带有explicit waitsCSS_SELECTOR单击 sing in 按钮:

Code:代码:

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

Imports:进口:

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

Screenshot:截屏:

在此处输入图像描述

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

相关问题 如何使用 Selenium WebDriver 单击该元素 - How to click that element with Selenium WebDriver 如何随机 select 并使用 selenium webdriver(Python)单击元素? - How to randomly select and click on element with selenium webdriver (Python)? 如何在selenium webdriver Python中用鼠标点击第二层元素? - How to click on the 2nd layer of element with mouse in selenium webdriver Python? 找到Python Selenium Webdriver元素,但无法单击它 - Python Selenium Webdriver element found but can not click on it 元素未在Odoo中单击python selenium webdriver中的单击 - Element is not clicking on click in python selenium webdriver in Odoo Selenium Webdriver (Python) - 单击 div 元素(复选框) - Selenium Webdriver (Python) - Click on a div element (checkbox) python selenium webdriver“没有这样的元素” - python selenium webdriver “no such element” 如何在 python 的 selenium Webdriver 中按索引查找元素 - How to find an Element by index in selenium Webdriver for python Python selenium webdriver - 无法通过xpath单击一个元素 - Python selenium webdriver - unable to click an element by xpath at a single go 带有 Python 的 Selenium Webdriver:元素不可点击且无法读取 null 的属性“点击” - Selenium Webdriver with Python: Element is not clickable & Cannot read property 'click' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM