Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bulldoze

macrumors regular
Original poster
Mar 15, 2011
229
51
Hello,

I am new to python and selenium webdriver and I am completely flummoxed by locating elements in a web page. For instance, I am trying to input a search term in the search box of the home page of macrumors: the code in the webpage has this section in it:

<div class="search">
<span class="icon"></span>
<form class="search-form" action="https://www.macrumors.com/search">
<input type="text" class="bginput" name="s" size="20" placeholder="Search


My python code is this:

Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()

# Go to Macrumors
driver.get('http://macrumors.com')
 
# Select the search box
python_link = driver.find_element_by_name('s')
python_link.send_keys('hello world!')

Does anyone know why this does not work? it opens Firefox and Macrumors but then does nothing and finished. Many thanks!
 
Last edited:
You should check what python_link is.

Better than using find_element_by_name, use find_elements_by_name (note the s, making it plural). How many elements are returned? If it's more than 1, I would imagine your problem is that your code is returning a different element than what you think it is.

Try using an interactive session of Python instead of just writing a script and running it. It's helpful with introspection and debugging.
 
This works for me :

Note : OS X 10.9.5 - Python 2.7.5 - selenium-2.44.0 - Firefox 25.0.1
 

Attachments

  • Screen Shot 2014-11-30 at 00.54.56.png
    Screen Shot 2014-11-30 at 00.54.56.png
    11.5 KB · Views: 186
Last edited:
I tried the interactive session and it worked fine.

I then run my script again and I think I just changed the Python_link variable to elem and it worked?!? I don't think I changed anything else.

Thanks for the help guys.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.