我試圖讓 Selenium 將我登錄到 Soundcloud,但 find_element_by_link_text 屬性對我不起作用,但它似乎適用于“了解更多”和“上傳您自己的”。為什么它適用于網頁上的某些元素而不適用于其他元素?
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()
uj5u.com熱心網友回復:
您嘗試查找的元素Sign in不在a要使用的標記中driver.find_element_by_link_text。它在一個button標簽中。
嘗試如下并確認:
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()
uj5u.com熱心網友回復:
德雷克波羅
find_element_by_link_text()
函式僅適用于包含在錨標記中的文本(有時稱為標記)。
下面是outerHTML對于Upload your own
<a href="/upload" class="frontContent__uploadButton sc-button sc-button-cta sc-button-primary sc-button-large">Upload your own</a>
請參閱此處,文本Upload your own包含在 HTML 中的標記中。
但是對于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>
它被包裹在一個按鈕標簽中。
所以這就是按鈕find_element_by_link_text()不起作用的原因 Sign In。
你可以css selector像這樣切換:
driver.find_element_by_css_selector('button.frontHero__loginButton')
或xpath using:
driver.find_element_by_xpath("//button[contains(@class,'frontHero__loginButton')]")
請注意,CSS 的優先級高于xpath。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324938.html
上一篇:無法定位元素Selenium-跳轉到下一個元素的方法
下一篇:將溫度轉換為int
