0

please can someone help me with this,

I can't get selenium to click a button with python. I'm on python 3.4 and using Firefox 42

the browser opens but that's all

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.speedyshare.com/")
elem = find_element_by_id_name("selectfilebox")
elem.click()

The browser opens but i get the following error

Traceback (most recent call last):
  File "/home/ro/sele.py", line 6, in <module>
    elem = find_element_by_id_name("selectfilebox")
NameError: name 'find_element_by_id_name' is not defined
5
  • This isn't a selenium problem, it's a problem with your Python imports. You need to tell Python how to find the find_element_by_id_name function. Commented Sep 27, 2015 at 18:30
  • right ok thanks, i'm sorry if I sound like a noob but did you mean elem = driver.find_element_by_name ? Commented Sep 27, 2015 at 18:37
  • Something like that. I don't know which module that function is in, I'll leave it up to you to find out. Commented Sep 27, 2015 at 18:42
  • You can take a look at the documentation section 4.1(for locating an element by id). Commented Sep 27, 2015 at 18:44
  • There is 'find_element_by_id_name()' function in python, it may be either 'find_element_by_id()' or 'find_element_by_name()'. You should use any one of these based on requirement. Commented Sep 28, 2015 at 4:10

1 Answer 1

6

It helps to inspect driver.page_source to see the HTML as the driver sees it.

driver.get("http://www.speedyshare.com/")
content = driver.page_source
with open('/tmp/out', 'wb', encoding='utf-8') as f:
    f.write(content)

You'll see in /tmp/out:

<frameset rows="*"><frame src="http://www30.speedyshare.com/upload_page.php" name="index31" />
</frameset>

Aha. The tag you wish to click is inside a frame. So switch to that frame first:

driver.switch_to.frame("index31")

and then you'll be able to find the element by id:

elem = driver.find_element_by_id("selectfilebox")
elem.click()

This question is essentially the same as Selenium Unable to locate element (Python) WebScraping; it's just hard to know that without first knowing the solution.

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

2 Comments

Thanks for this, you really went in depth and it was very insightful, sorry I've took my time to reply but I wanted to get it working and not bug anyone to be spoonfed the info. I've come up with this but it's still not working.
The idea of looking at page source is great.

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.