4

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

Camilo
  • 419
  • 1
  • 4
  • 9
  • show your code. What do you want to do with this variable? Open a site? – vitaliis May 20 '21 at 03:22
  • sorry, now there is the full code – Camilo May 20 '21 at 03:41
  • Do you want to click the link facebook.com? – vitaliis May 20 '21 at 03:43
  • yes, I want to click that link. the file choice.txt in def url_random(): only has a line of text which is facebook.com – Camilo May 20 '21 at 03:50
  • 1
    Check the answer. If it works, accept it. – vitaliis May 20 '21 at 03:53
  • 1) Run both of your scenarios 10 times and see if it is consistent. If it's not, then you probably have a timing issue. 2) Put a `print(fb_url)` before you click it. It may be that `url_random()` is not returning what you think. – JeffC May 20 '21 at 03:54
  • I'd like to second JeffC:s option 2, with one addition: make it `print(repr(fb_url))`; that way it will be easier to see exactly what fb_url contains, which might be enlightening. – Ture Pålsson May 20 '21 at 04:03
  • it prints facebook.com. Already added more time, 5 seconds, 10 seconds, 20 seconds, but is not able to find it – Camilo May 20 '21 at 04:08
  • `'facebook.com\n'` is the output from `print(repr(fb_url))` – Camilo May 20 '21 at 04:10

1 Answers1

3

To click Facebook.com link use xpath:

driver.find_element_by_xpath("//a[contains(@href, 'facebook.com')]").click()

Explanation: facebook.com is the value of href attribute.

In the context of your task it would look like:

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

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get("https://testingcodeclicks.blogspot.com/2021/05/facebook.html")
fb_url = "facebook.com\n"
url = fb_url.rstrip()
locator = f"//a[contains(@href, '{url}')]"
#  print(type(locator))
#  print(type(url))
#  print(locator)
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, locator)))
driver.find_element_by_xpath(locator).click()
# driver.close()
# driver.quit()

I edited my answer. facebook.com was not passed as string to locator. I left debugging code commented for you to check it by yourself. Also, I added rstrip() to fb_url. It cuts \n. If even it won't work, try using strip(). Check here for more details: How can I remove a trailing newline?

vitaliis
  • 4,082
  • 5
  • 18
  • 40
  • This doesn't answer the question... the question is why isn't the element being clicked when passed using a variable. That is not addressed by your answer. – JeffC May 20 '21 at 03:55
  • I get selenium.common.exceptions.ElementNotVisibleException: Message: element not visible – Camilo May 20 '21 at 04:01
  • Try #2 from JeffC comment. What is returned? There must be a string "facebook.com" – vitaliis May 20 '21 at 04:02
  • @Camilo Try `print(type(fb_url))` – vitaliis May 20 '21 at 04:08
  • 1
    `` is the output from `print(type(fb_url))` – Camilo May 20 '21 at 04:12
  • the #2 from JeffC comment returns `'facebook.com\n'` with `print(repr(fb_url))` – Camilo May 20 '21 at 04:19
  • Use `fb_url.rstrip()` to cut \n. \n means new line. – vitaliis May 20 '21 at 04:27
  • `fb_url.rstrip()` worked and the new edit that you added worked really well and it solves the problem but only if I try it with `fb_url = "facebook.com\n"`, but when I tried to add `fb_url = url_random()` ,because I need to generate a random link from the list and visit it, I get the output raise `TimeoutException(message, screen, stacktrace)` from the line `WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, locator))) ` I am trying to find out why but the element is already loaded and visible – Camilo May 20 '21 at 04:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232620/discussion-between-vitaliis-and-camilo). – vitaliis May 20 '21 at 04:59