I am able to find and click an href with this method and passing the string "facebook.com"
driver.find_element_by_link_text("facebook.com")
but when I use a variable to store the string like this
fb_url = "facebook.com"
and then I try to pass it like this
driver.find_element_by_link_text(fb_url)
I get this output
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"facebook.com"}
I already tested it with driver.find_element_by_partial_link_text
, driver.find_element_by_xpath
, driver.findElement(By.linkText())
but always get the same results, they work by passing the string "facebook.com"
but if I pass the variable fb_url
it fails. I already searched for similar questions but none of the answers seem to work, I have tried them all.
full code
from selenium import webdriver
import time
import random
def url_random():
with open("choice.txt") as f:
lines = f.readlines()
aleatorio = random.choice(lines)
return aleatorio
def search_url():
driver = webdriver.Chrome()
driver.get('https://testingcodeclicks.blogspot.com/2021/05/facebook.html')
time.sleep(3)
fb_url = url_random()
driver.find_element_by_link_text(fb_url).click()
search_url()
the file choice.txt
in def url_random():
only has a line of text which is facebook.com