Find element and select option with phantomjs - python

I want to find an element by name and select an option from a drop-down menu by value with phantomjs. The following script
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.PhantomJS()
driver.get("http://www.wikipedia.org/")
select = Select(webdriver.find_element_by_name("language"))
select.select_by_value("es")
html_doc = driver.page_source
driver.quit()
generate the error
Traceback (most recent call last):
File "test.py", line 7, in <module>
select = Select(webdriver.find_element_by_name("language"))
AttributeError: 'module' object has no attribute 'find_element_by_name'
If I change webdriver.PhantomJS() I to webdriver.Firefox() I get the same error. What am I doing wrong? The module is not correctly installed?

webdriver is the module name you have imported, while driver is your WebDriver instance.
Change
select = Select(webdriver.find_element_by_name("language"))
^^^^^^^^^
to
select = Select(driver.find_element_by_name("language"))
^^^^^^

Related

Python - Web Scraping - Selenium - AttributeError: 'WebDriver' object has no attribute 'find_elements_by_xpath

I wrote a code in Python for Web Scraping and fetching HTML table but its throwing an Attribute Error : 'WebDriver' object has no attribute 'find_elements_by_xpath'
FULL ERROR
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe')
Traceback (most recent call last):
File "C:\Users\rajat.kapoor\PycharmProjects\RajatProject\FirstPythonFile.py", line 6, in
scheme = driver.find_elements_by_xpath('//tbody/tr/td[0]')
Given Below is the Code
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe')
driver.get('https://www.mutualfundssahihai.com/en/schemeperformance')
driver.maximize_window()
scheme = driver.find_elements_by_xpath('//tbody/tr/td[0]')
benchmark = driver.find_elements_by_xpath('//tbody/tr/td[1]')
result=[]
for i in range(len(riskometer)):
temporary_data = {'Scheme':scheme.text,
'Benchmark':benchmark.text}
result.append(temporary_data)
df_data = pd.DataFrame(result)
df_data.to_excel('scrapingresult.xlsx',index=False)
I tried writing the code for Web Scraping using Selenium (fetch HTML Table) but its throwing an Attribute Error :'WebDriver' object has no attribute 'find_elements_by_xpath'
FULL ERROR
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe')
Traceback (most recent call last):
File "C:\Users\rajat.kapoor\PycharmProjects\RajatProject\FirstPythonFile.py", line 6, in
scheme = driver.find_elements_by_xpath('//tbody/tr/td[0]')
Below is the code for the same
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe')
driver.get('https://www.mutualfundssahihai.com/en/schemeperformance')
driver.maximize_window()
scheme = driver.find_elements_by_xpath('//tbody/tr/td[0]')
benchmark = driver.find_elements_by_xpath('//tbody/tr/td[1]')
result=[]
for i in range(len(riskometer)):
temporary_data = {'Scheme':scheme.text,
'Benchmark':benchmark.text}
result.append(temporary_data)
df_data = pd.DataFrame(result)
df_data.to_excel('scrapingresult.xlsx',index=False)
Updated
The same issue can be seen here TypeError: 'module' object is not callable ( when importing selenium ).
The line,
driver = webdriver.chrome('C:\webdrivers\chromedriver.exe')
should be,
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe')
notice the capital 'C' in Chrome.
Additionally use
driver.find_element("xpath", "#path_selector")
as find_elements_by_xpath is removed.

How do I fill out a form using Python and Selenium?

I am new to Python and wanted to use it for automatic login. I found https://automatetheboringstuff.com/chapter11/ and tried:
#! python3
from selenium import webdriver
browser = webdriver.Firefox()
type(browser)
browser.get('https://forum-studienstiftung.de/')
emailEl = browser.find_element_by_id(username)
Unfortunately, this leads to:
Traceback (most recent call last): File "", line 1, in
emailEl = browser.find_element_by_id(username) NameError:
name 'username' is not defined
According to the Firefox Developer Tools the correct ID is "username".
Wrap username in quotation marks. Right now you are passing in a variable called username which selenium is trying to match with an id on the page with the same value. Since the value is none, Selenium cannot find it hence the error.
The page you are trying to access takes time to load. You have to wait for the element to be visible before accessing it.
Try this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
type(browser)
browser.get('https://forum-studienstiftung.de/')
emailEl = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.id, "username")))

Why am I getting NameError: name 'ActionChains' is not defined?

Im pretty new to python and trying to fill out a web form automated.
Im getting this Error:
Traceback (most recent call last):
File "main.py", line 24, in
ActionChains(browser)\
NameError: name 'ActionChains' is not defined
And this is my code:
from time import sleep
from selenium import webdriver
browser = webdriver.Chrome ('/Users/max/Downloads/chromedriver')
browser.get ('http://www.brix.de/computer/web_html_php_et_al/formular-test_smm_01.html')
inputs = browser.find_element_by_xpath(
'/html/body/form[1]/table')
ActionChains(browser)\
.move_to_element(input[vorname]).click()\
.send_keys('name')\
.move_to_element(input[name]).click()\
.send_keys('Surname')\
.perform()
Can somebody help me please?
I think you are missing the import, try the following:
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome ('/Users/kiran/Downloads/chromedriver')
browser.get ('http://www.brix.de/computer/web_html_php_et_al/formular-test_smm_01.html')
inputs = browser.find_element_by_xpath(
'/html/body/form[1]/table')
ActionChains(browser)\
.move_to_element(input[name]).click()\
.send_keys('name')\
.move_to_element(input[vorname]).click()\
.send_keys('Surname')\
.perform()

driver.get on selenium giving error

I have tried running the following command after I've imported everything I need (selenium, webdriver, keys):
>>> driver.get('https://steemit.com/')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'selenium.webdriver.chrome' has no attribute 'get'
I can't find any solution.
You are trying to call .get() method on a module. Instead, you need to instantiate a webdriver:
In [1]: from selenium import webdriver
In [2]: driver = webdriver.Chrome()
In [3]: driver.get('https://steemit.com/')
In [4]: print(driver.title)
Trending posts — Steemit
To avoid further selenium module usage confusion, please go through the "Get Started" section of the Python/Selenium documentation.
from your comment it looks like your code is:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.chrome # should be webdriver.chrome()
driver.get('')
and your traceback is:
Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'selenium.webdriver.chrome' has no attribute 'get'
which you are missing the instantiation of the driver, also as pointed out below chrome should be capitalised, so this:
driver = webdriver.chrome
needs to become:
driver = webdriver.Chrome() # the brackets mean the object is created or 'instantiated'

TypeError: 'module' object is not callable ( when importing selenium )

i have problem when running this code :
>>> from selenium import webdriver
>>> driver = webdriver.firefox()
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
driver = webdriver.firefox()
TypeError: 'module' object is not callable
i have searched for the problem and i got some results. but unfortunately , they didn't work. So , how can i solve this?
thanks.
You have made a typo.
webdriver.Firefox()
Note the capital F.
the same goes for other browsers!
e.g.
webdriver.chrome Vs. webdriver.Chrome
(its even harder to notice this!)
thanks so much for the help! ;)
Another way is:
from selenium.webdriver import Chrome.
driver = Chrome()
When typing "Chrome" Note the capital C.
You probably gonna need to specify the executable_path for chromedriver.exe:
driver = Chrome(executable_path="path_in_here")
This error message...
TypeError: 'module' object is not callable
......implies that your program is trying to call a python module.
You need a minor modification in the offending line of code. You have used:
driver = webdriver.firefox()
Where as firefox is a module for example as in:
selenium.webdriver.firefox.options
So you have to change firefox() to Firefox() and your effective line of code will be:
driver = webdriver.Firefox()
Likewise:
For Chrome:
driver = webdriver.Chrome()
For Internet Explorer:
driver = webdriver.Ie()

Categories