python - python selenium如何点击登录按钮
问题描述
<a href="/?redirectURL=%2F&returnURL=https%3A%2F%2Fpartner.yanolja.com%2Fauth%2Flogin&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)
解决方案
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()
推荐阅读
- spring - ignoreUnresolvablePlaceholders 不起作用
- angular - 在指令实例上应用方法
- php - api中的Laravel Restclient 500错误
- ios - 无法在 Bitrise 上运行 iOS 的 UI 测试
- selenium - 窗口选项卡自行关闭时测试脚本出错
- html - 我需要将图像形状更改为梯形而不裁剪图像
- jsp - 如何在 JSP 中正确设置子域的 cookie?
- javascript - 使用“清理”按钮重置(清除)输入字段 JS/HTML
- javascript - 如何在输入文件中动态添加文件?
- webpack - 我如何忽略 webpack 编译中的一些文件夹/文件