Home > OS >  Python Selenium find_element_by_link_text not working for me
Python Selenium find_element_by_link_text not working for me

Time:10-17

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

CodePudding user response:

The Element you are trying to find Sign in is not in a a tag to use driver.find_element_by_link_text. Its in a button tag.

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

CodePudding user response:

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

<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.

But for Sign In button :

<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.

You can switch to css selector like this :

driver.find_element_by_css_selector('button.frontHero__loginButton')

or xpath using :

driver.find_element_by_xpath("//button[contains(@class,'frontHero__loginButton')]")

Note that CSS has more precedence than xpath.

  • Related