[英]Python Selenium find_element_by_link_text not working for me
I am trying to have Selenium log me into Soundcloud but the find_element_by_link_text attribute isn't working for me but it seems to work for "Find out more" and "Upload your own".我试图让 Selenium 将我登录到 Soundcloud,但 find_element_by_link_text 属性对我不起作用,但它似乎适用于“了解更多”和“上传您自己的”。 Why is it working for some of the elements on the web page but not others??
为什么它适用于网页上的某些元素而不适用于其他元素?
from tkinter import *
import random
import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import requests
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
driver = webdriver.Chrome(executable_path='/Users/quanahbennett/PycharmProjects/SeleniumTest/chromedriver')
url= "https://soundcloud.com/"
driver.get(url)
#time.sleep(30)
wait = WebDriverWait(driver, 10)
link = driver.find_element_by_link_text('Sign in');
link.click()
breakpoint()
The Element you are trying to find Sign in
is not in a a
tag to use driver.find_element_by_link_text
.您尝试查找的元素
Sign in
不在用于使用driver.find_element_by_link_text
a
标签中。 Its in a button
tag.它在一个
button
标签中。
Try like below and confirm:尝试如下并确认:
driver.get("https://soundcloud.com/")
wait = WebDriverWait(driver,30)
# Cookie pop-up
wait.until(EC.element_to_be_clickable((By.ID,"onetrust-accept-btn-handler"))).click()
# Click on Sign-in
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='frontHero__signin']//button[@title='Sign in']"))).click()
drakepolo,德雷克波罗
find_element_by_link_text()
function only work for text which are enclosed in anchor tag (sometimes they are called a tag).函数仅适用于包含在锚标记中的文本(有时称为标记)。
Below is the outerHTML
for Upload your own
下面是
Upload your own
的outerHTML
<a href="/upload" class="frontContent__uploadButton sc-button sc-button-cta sc-button-primary sc-button-large">Upload your own</a>
See here the text Upload your own
is in enclosed in a tag in HTML.请参阅此处的文本“
Upload your own
内容”包含在 HTML 标记中。
But for Sign In
button :但是对于
Sign In
按钮:
<button type="button" class="g-opacity-transition frontHero__loginButton g-button-transparent-inverted sc-button sc-button-medium loginButton" tabindex="0" title="Sign in" aria-label="Sign in">Sign in</button>
it is wrapped inside in a button tag.它被包裹在一个按钮标签中。
So that is the reason find_element_by_link_text()
will not work for Sign In
button.所以这就是
find_element_by_link_text()
不适用于Sign In
按钮的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.