1

I want to get the following element but I'm struggling to do so. I've tried by class, css_selector, etc.

<a href="http://www.google.ca" target="_blank" class="btn visit__link">
"Visit this Webpage"
<br>
"for more content!"
</a>
<br>
<small>Opens in a new window.</small>
</div>
</section>

I've tried the following code, but it doesn't seem to work:

web = browser.find_element(By.cssSelector("a[class='btn visit__link']")).click()
3
  • have you tried By.cssSelector('.visit__link')? Commented Jul 16, 2015 at 17:21
  • This doesn't look like the Python driver of Selenium. Commented Jul 16, 2015 at 17:27
  • 1
    browser.find_element_by_class_name('visit__link') Commented Jul 16, 2015 at 17:29

2 Answers 2

1

One option would be to locate the link by the class name as @Malik suggested:

browser.find_element_by_class_name('visit__link')

or with a CSS selector:

browser.find_element_by_css_selector('a.visit__link')

Or, you can also locate the link by the partial link text:

browser.find_element_by_partial_link_text('Visit this Webpage')

You may also need to wait for the button to become visible:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(browser, 10)

link = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Visit this Webpage")))
link.click()
Sign up to request clarification or add additional context in comments.

2 Comments

Why am I getting this error: wait = WebdriverWait(browser, 10) NameError: name 'WebdriverWait' is not defined
@Throwaway012 sure, it was a typo, fixed.
0

Try this selector:

By.cssSelector("a[class*='visit__link']")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.