-1

I have been stuck on trying to click the href="javascript:;" on a page using selenium with python.

The relevant HTML code is:

<tr>
  <td class="Sun out">24</td>
  <td class=" out">25</td>
  <td class=" out">26</td>
  <td class=" out">27</td>
  <td class=" out">28</td>
  <td id="CellPlayDate0" class=" able"><a href="javascript:;" onclick="fnSelectPlayDate(0, '20171229')">29</a></td>
  <td id="CellPlayDate1" class=" able"><a href="javascript:;" onclick="fnSelectPlayDate(1, '20171230')">30</a></td>
</tr>

For code, I have:

browser.find_elements_by_xpath('//*[@id="CellPlayDate0"]').click()

The corresponding error message is:

AttributeError: 'list' object has no attribute 'click'

I think that the elements is list, so i tried as below

browser.find_elements_by_xpath('//td[@id="CellPlayDate1"]')[0].click()

However, another error

IndexError: list index out of range

I think there is another method to click a button(javascript,onclick). However, I am the very beginner of selenium, so i have no idea.

Any ideas?

0

4 Answers 4

1

It seems browser.find_elements_by_xpath('//*[@id="CellPlayDate0"]') returns a list of elements.

You need to pick an element and create click event may be like

browser.find_elements_by_xpath('//*[@id="CellPlayDate0"]')[0].click()

Sign up to request clarification or add additional context in comments.

7 Comments

I've tried that. However, there is an IndexError: list index out of range.
In that case your xpath expression '//*[@id="CellPlayDate0"]' does not match any id in the html page.
Actually, I copy that html code in a page using copy xpath
Could you please edit the original question with snippet of html page?
Could you check again
|
0

The error says it all :

AttributeError: 'list' object has no attribute 'click'

As per your code block you have :

browser.find_elements_by_xpath('//*[@id="CellPlayDate0"]').click()

Which instead of an WebElement returns a List. Hence when you try to click on the List you see the error as AttributeError: 'list' object has no attribute 'click'

Solution would be either to change the method find_elements_by_xpath to find_element_by_id or to construct an unique xpath as follows:

  • As the WebElement have a id:

    browser.find_elements_by_id("CellPlayDate0').click()
    
  • Construct an xpath:

    browser.find_elements_by_xpath("//td[@id='CellPlayDate0']").click()
    

Comments

0

There problem here is you're calling click() on a list of <td> instead of the <a> anchor which actually has the click you wanted. Try this instead:

browser.find_element_by_xpath('//*[@id="CellPlayDate0"]/a').click()

Notice the use of find_element_by_xpath instead of find_element[s]_by_xpath? Or with css:

browser.find_element_by_css_selector('#CellPlayDate0 a').click()

1 Comment

Still, there is the same error, AttributeError: 'list' object has no attribute 'click'
0

You have two problems.

First, the xpath expression returns a list as the other answers have pointed out. Python lists don't have a click method, the returned elements do.

Second, your xpath expression isn't actually finding anything. It's returning an empty list of no matches, which is why you're getting the index out of range error.

What you probably want is this:

for elem in browser.find_elements_by_xpath('//*[@id="CellPlayDate0"]'):
    elem.click()

Note that you'll still need to tweak the xpath string to actually match something on the page.

1 Comment

There is no error. However, the button is not clicked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.