I am getting the exception while running below code - python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.chrome(executable_path="C:\Driver\chromedriver_win32\chromedriver.exe")
driver.get("www.youtube.com")
print (driver.title)
driver.close()
for the above code I'm getting the error
TypeError: 'module' object is not callable

I recommend modular programming.
Set a function that returns the chrome driver like such:
# Opens chrome driver
def openChrome():
# directory to chromedrive
chromeDriver = "C:\Driver\chromedriver_win32\chromedriver.exe"
return webdriver.Chrome(chromeDriver)
Call this function like this:
url = "https://stackoverflow.com/"
driver = openChrome()
driver.get(url)
So in the future if your driver isn't working, you don't have to change it on every script you will change it in one place (function)

Related

I am facing trouble launching the browser in selenium python

This is what I wrote below. I am using PyCharm 2022.2.2 (Community Edition)
Build #PC-222.4167.33, built on September 15, 2022
from selenium import webdriver
# Chrome driver = Path
#to open an URL in a BROWSER
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
name = "Trainer"
service_obj = Service(r"C:\Users\DS-02\Desktop\Manjit\chromedriver.exe") #---- doesn't works
driver = webdriver.Chrome(Service = service_obj)
The error which I am getting is below:
C:\Users\DS-02\AppData\Local\Programs\Python\Python310\python.exe C:\Users\DS-02\Desktop\Manjit\PythonSelenium\Class_1_demo_Browser.py
Traceback (most recent call last):
File "C:\Users\DS-02\Desktop\Manjit\PythonSelenium\Class_1_demo_Browser.py", line 10, in <module>
driver = webdriver.Chrome(Service = service_obj) #----- doesn't works
TypeError: WebDriver.__init__() got an unexpected keyword argument 'Service'
Please help,
Thanks and regards
I have tried the code below and works sometime now it doesn't works. I want to fix the code above:
#driver = webdriver.Chrome(executable_path = service_obj)
#driver = webdriver.Chrome(executable_path = '../Webdriver/chromedriver.exe')
Please help
You need to change this line driver = webdriver.Chrome(Service = service_obj) to be
driver = webdriver.Chrome(service = service_obj)
Parameter to be passed into webdriver.Chrome() in order to initialize driver object should be lowercased service while Service in your code is this:
selenium.webdriver.chrome.service, you called it Service here:
from selenium.webdriver.chrome.service import Service
This error message...
TypeError: WebDriver.__init__() got an unexpected keyword argument 'Service'
...implies that when the program execution starts the int() method counters an unexpected keyword argument 'Service'.
Details
With the availability of selenium4 the key executable_path() is deprecated and you have to use the key service.
However you need to take care of couple of things as follows:
Incase you use the service keyword you have to create a Service object and assign the absolute location of the ChromeDriver, you have to import:
from selenium.webdriver.chrome.service import Service
The keyword within webdriver.Chrome() is service. So you have to:
service_obj = Service('C:\\Users\DS-02\\Desktop\\Manjit\\chromedriver.exe')
driver = webdriver.Chrome(service = service_obj)

TypeError: get() missing 1 required positional argument: 'url' error using GeckoDriver and Firefox through Selenium and Python

Executing below code in pycharm.
from selenium import webdriver
browser = webdriver.Firefox
browser.get('https://www.google.com')
Error:
TypeError: get() missing 1 required positional argument: 'url'
How can I solve the error?
Specify the path the chrome driver is located in for example when calling
webdriver.Firefox(‘C://Users/Username/Downloads/‘)
This worked for me:
from selenium import webdriver
driver = webdriver.Chrome("C:\\Users\Rishabh\Downloads\chromedriver_win32\chromedriver.exe")
driver.get('https://web.whatsapp.com/')
Alternate code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\Users\Rishabh\Downloads\chromedriver_win32\chromedriver.exe")
driver.get('https://web.whatsapp.com/')
In my case, I got this error for not using parenthesis ().
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.google.com')
Try with braces while creating Firefox instance. see below example.
from selenium import webdriver
browser = webdriver.Firefox() #focus on () at the end
browser.get('https://www.google.com')
The constructor is driver = webdriver.Firefox(). So in your code block you need to replace driver = webdriver.Firefox with:
driver = webdriver.Firefox()
Additionally, you may need to pass the absolute path of the GeckoDriver binary as follows:
driver = webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')
The issue is caused bacause there are not,()pharentesis, put the pharantesis end to the line
Check this
In Selenium and python
driver = webdriver.Chrome()

Separate driver file for browser selenium python base

I tried to have a separate file for browser driver, in my driver file with a file name driver.py:
from selenium import webdriver
class MyDriver():
def __init__(self):
self.driver = webdriver.Firefox()
I try reusing it on another file here's the code:
from driver import MyDriver
driver = MyDriver()
driver.get('http://google.com')
However, I always got an error:
AttributeError: 'MyDriver' object has no attribute 'get'
You simply forgot to add another driver. Try this:
driver.driver.get('http://google.com')

Selenium Python - Getting the current URL of web browser?

I have this so far:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('C:\Users\Fan\Desktop\chromedriver.exe')
url = driver.current_url
print url
It keeps saying that line 4 "driver" is an invalid syntax. How would I fix this?
Also is there a way I can get all the current tabs open, and not just a single one?
EDIT: the above code works now; But I have another problem!
The code now opens a new tab, and for some reason the URL bar has "data;" in it, and it outputs data; as the print.
But I want it to take the existing URL from existing web browser already opened, how do I solve this?
In Python you do not specify the type of variable as is required in Java which is the reason for the error. The same error will also happen because your last line starts with String.
Calling webdriver.Chrome() returns a driver object so the line webdriver driver = new webdriver() is actually not needed.
The new keyword is not used in Python to create a new object.
Try this:
from selenium import webdriver
driver = webdriver.Chrome()
url = driver.getCurrentUrl()
In order to extract the url of the current page from the web driver you have to call the current_url attribute:
from selenium import webdriver
import time
driver = webdriver.Chrome()
#Opens a known doi url
driver.get("https://doi.org/10.1002/rsa.1006")
#Gives the browser a few seconds to process the redirect
time.sleep(3)
#Retrieves the url after the redirect
#In this case https://onlinelibrary.wiley.com/doi/abs/10.1002/rsa.1006
url = driver.current_url

Can't send keys selenium webdriver python

Trying to execute simple test
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
driver.find_element_by_name('q')
driver.send_keys('hey')
And getting the error
Traceback (most recent call last):
File "C:/webdriver/test.py", line 8, in <module>
driver.send_keys('hey')
AttributeError: 'WebDriver' object has no attribute 'send_keys'
What's the problem?
WebDriver instance does not have send_keys() method. That's what the error is actually about:
'WebDriver' object has no attribute 'send_keys'
Call send_keys() on a WebElement instance which is returned by find_element_by_*() methods - find_element_by_name() in your case:
element = driver.find_element_by_name('q')
element.send_keys("hey")
And just FYI, there is also an ActionChains class which is useful do build up chains of actions or apply more complex actions like drag&drop or mouse move. It's an overhead in this case, but just for the sake of an example:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element(element).send_keys("hey").perform()
You must change the reference element
driver.get('http://google.com')
elem.find_element_by_name('q')
elem.send_keys('hey')
Did you try changing your reference element.
It would be a good practice if you call webdriver using different reference.
Your code after changing reference.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get('http://google.com')
#Firefox Webdriver is created and navigated to google.
elem = driver.find_element_by_name('q')
elem.send_keys('hey',Keys.RETURN)
#Keys.RETURN = Pressing Enter key on keyboard
time.sleep(5)
driver.close()
I had faced same issue, but I got solution for that.
Use move_to_element in ActionChains.
Find element. elem = driver.find_element_by_*
create your actionchain driver actions = ActionChains(driver)
use move to command, so webdriver will point to that element's position, but before you send key, you need to set webdriver position on that element by using click() function. Now, webdriver has a surface area to place given keys (data) and for that you will use send_keys() function. In the last just put perform() function to execute these all task very smoothly.
actions.move_to_element(<ELEMENT>).click().send_keys(<DATA>).perform()
https://selenium-python.readthedocs.io/api.htmlt
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
elem = driver.find_element_by_name('q')
actions = ActionChains(driver)
actions.move_to_element(elem).click().send_keys('hey').perform()
The problem with your code is you have not tell driver where to send the keys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
driver.find_element_by_name('q')
driver.send_keys('hey') // driver dont know where to send the keys
correct code would be :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
goog = driver.find_element_by_name('q') // you have stored element in goog variable
goog.send_keys('hey') // you told variable to send hey
or
you can directly send after finding the element like :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
driver.find_element_by_name('q').send_keys('hey')
If it helps so please accept the answer !!
Its with the version of selenium which is causing the issue. I faced the same issue.
Its with the selenium version 3.3.3 which has the compatibility problem.
Try:
pip uninstall selenium
pip install selenium==3.3.1
Hope it works.

Categories