登录按钮,python,html,css,selenium"/>

首页 > 解决方案 > python selenium如何点击登录按钮

问题描述

<a href="/?redirectURL=%2F&amp;returnURL=https%3A%2F%2Fpartner.yanolja.com%2Fauth%2Flogin&amp;serviceType=PC" aria-current="page" class="v-btn--active v-btn v-btn--block v-btn--depressed v-btn--router theme--light v-size--large primary" data-v-db891762="" data-v-3d48c340=""><span class="v-btn__content">
    login
  </span></a>

我想使用 python-selenium 点击这个 href 按钮。

首先,我尝试使用find_element_by_xpath(). 但是,我看到一条错误消息是no such element: Unable to locate element. 然后,我使用了 link_text、partial_link_text 等等。

我认为该消息说“找不到登录按钮”对吗?如何指定登录按钮?

我是第一次使用 selenium,我的 html 知识也不够。我应该先学习什么?

+)

网址:https ://account.yanolja.bz/

我想登录并获取此 URL 的登录会话。

from urllib.request import urlopen
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

html = urlopen('https://account.yanolja.bz/')
id = 'ID'
password = 'PW'

#options = webdriver.ChromeOptions()
#options.add_argument("headless")
#options.add_argument("window-size=1920x1080")
#options.add_argument("--log-level=3")

driver = webdriver.Chrome("c:\\chromedriver")
driver.implicitly_wait(3)
driver.get('https://account.yanolja.bz/')

print("Title: %s\nLogin URL: %s" %(driver.title, driver.current_url))
id_elem = driver.find_element_by_name("id")
id_elem.clear()
id_elem.send_keys(id)

driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys(password)

标签: pythonhtmlcssselenium

解决方案


span由于您的a标签,这很可能无法正常工作。我试图给出一些例子,但我不确定你是否应该点击a标签或span. 我试图点击span所有这些。
我希望它确实有效。只有你能把你尝试过的东西给我们,这将有助于找出错误,如果有的话。

您是否尝试过使用类查找元素。您的链接由这么多类命名,它们都不是唯一的吗?
driver.find_element(By.CLASS_NAME, "{class-name}").click()

使用 x 路径:
driver.find_element_by_xpath("//span[@class='v-btn__content']").click() driver.find_element_by_xpath("//a[@class='{class-name}']/span[@class='v-btn__content']").click()

如果这个 xpath 不是唯一的,那么你可以使用 css 选择器
driver.find_element_by_css_selector("a[aria-current='page']>span.v-btn__content").click()


推荐阅读