0

I am trying to click a particular button with Selenium in Python 3.6.but button is not working.

Inspect element ;

<span class="jsgrid-pager-page"><a href="javascript:void(0);">2</a></span>

I tried this code;

page_button = browser.find_element_by_class_name("2").click()

what should I do?

3
  • Why are you looking for the class name "2"? Do you have such a class? Commented Jun 12, 2017 at 7:04
  • Because total page number is 6 but I would like to get the information found on Page 2. Commented Jun 12, 2017 at 7:09
  • I added some explanation on my answer about why this didn't work and a post about locators that will help you learning, I hope it helps :) Commented Jun 12, 2017 at 7:18

4 Answers 4

2

You might use link text instead of class name as '2' is not class name for any tag in your HTML code. You can try below code

page_button = browser.find_element_by_link_text("2").click();
Sign up to request clarification or add additional context in comments.

1 Comment

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"2"}
0

There is no element in your example that will be found by:

browser.find_element_by_class_name("2").click()

You are searching now for a element with the class name 2, so something like <span class="2">

You can target the span by the class jsgrid-pager-page, but there wil possibly be more the one occurrence of the class name.

An other possibility is to target the element by xpath, so like:

//span/a[text()='2'] 

page_button = browser.find_element(By.XPATH, "//span/a[text()='2']").click()

This way the the driver is going to search for a section containing span and an anchor and a 2.

So when you want to target the first of the third one in the row you just change the number

// First
page_button = browser.find_element(By.XPATH, "//span/a[text()='2']").click()
// Third
page_button = browser.find_element(By.XPATH, "//span/a[text()='2']").click()

I suggest you read something about locators it wil help you in the future: Locators

6 Comments

Your XPath is incorrect: 1) You should use //span instead of just span, 2) You use double quotes two times in a single string: both for XPath and text value- this will cause an error. You should mix single/double quotes. Also it's bad idea to use "contains(text(), '2')" as it will match all links that contains "2" text.
Great catch, I overlooked it, and updated!. your are right about the match, but is more exact than find_element_by_link_text("2"). because the xpath only matches on a <span><a>2</a></span>
find_element_by_link_text("2") should match exactly the link with text content "2" while find_element_by_xpath("//span/a[contains(text(), '2')]") will match link with text content "22", "something 2" and other links that OP doesn't looking for. If you want to use XPath, you'd better use more explicit predicate [text()='2']
Again you are right, I will update my answer, so it will be more correct
I copied xpath and result is : xpath like this : //*[@id="jsGrid"]/div[3]/div/span[4]/a How can I configure "find_element_by_xpath"
|
0

If by

but button is not working.

you mean that the button is not redirecting to some page or doing something, than it might be explained by the href="javascript:void(0) attribute inside your a tag. The void operator after evaluating an expression would return undefined in this case, and so the browser will stay in the same page and button click would not do anything.

Please see this SO post for more information about href="javascript:void(0)


This is apart from the fact that your locator is not correct. Please fix the locator first.

Comments

0

There is no class name "2" added to target link HTML. You might use link text to locate required element. If with answer provided by @Ankur Singh you get NoSuchElementException it might mean that link generated dynamically and you should wait for its appearance in DOM:

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

wait(browser, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "2"))).click()

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.