Python - Importing Libraries Properly - python

Question.
I am a noob, what is the difference between this:
import selenium
And this
from selenium import webdriver.
Doesn't import selenium automatically import webdriver?

Yes it does but the use is different.
When you type from selenium import webdriver you can access it by typing webdriver in your code.
But when you run import selenium you can access webdriver by typing selenium.webdriver.
Sometimes you want to make clear where this function is from like in django urls I always type from . import views so I can then type views.myfunction in code.

For import selenium, names are accessed via selenium.nameX. You also have access to the entire selenium package.
With from selenium import webdriver, you are importing only webdriver from the selenium package. You do not have access to the entire selenium package. In the second case, you also access names from webdriver via webdriver.nameX, not selenium.webdriver.nameX.

yes but if you just import selenium you would have to do selenium.webdriver.functionyouwant instead of just webdriver.functionyouwant

When you type import package, you can access module by typing package.module.
When you type from package import module, you can access module by typing module and you can't use other modules in package package.

Related

org.openqa.selenium.JavascriptExecutor could be resolved

I was trying to insert a js code into a website using selenium.
I know that I want to use the following import statement,
import org.openqa.selenium.JavascriptExecutor
But this was not installed,
"org.openqa.selenium.JavascriptExecutor" is not accessed
Import "org.openqa.selenium.JavascriptExecutor" could not be resolved
where to install the module or how to resolve the problem
You have tagged python and using the Selenium-Python clients you don't require any additional import other then from selenium import webdriver as both the methods to execute JavaScript synchronously and asynchronously i.e.
execute_script()
execute_async_script()
are supported out of the box.
Where as incase you are using the Selenium-Java clients you additionally need to import:
import org.openqa.selenium.JavascriptExecutor;

ImportError: cannot import name 'log' from 'webdriver_manager.logger'

I have been trying to mess around with some Selenium stuff in PyCharm, and I have come across the error:
ImportError: cannot import name 'log' from 'webdriver_manager.logger'
Which is a result of the copied code:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
However the error comes up in the package, from the line of code:
from webdriver_manager.logger import log
From looking online it seems that this is because there is another name that clashes with it? But in all honesty I have no idea why its messing up.
Any help would be greatly appreciated!
the webdriver_manager package in not install correctly, so you just need to uninstall and reinstall webdriver_manager package.
the list of webdriver_manager are:
webdriver-manager
py-webdriver-manager
for more certainly:
scrapy-webdriver
after that, your issues will be solved

I am getting error while importing a file.So How do i solve?

I am getting below error code.I don't have any much experience about python.Please help me.
ImportError: cannot import name 'geckodriver' from 'selenium' (C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\__init__.py)
PS C:\Users\Administrator>
The error says you are trying to do something like
from selenium import gekodriver
There is no gekodriver method in selenium class ,. You should use
from selenium import webdriver
driver = webdriver.Firefox()
If you want to open firefox

Selenium / Python - No module named 'org'

I am trying to use the Select function in Selenium for Python 3 to help with navigating through the drop down boxes. However, when I try to import org.openqa.selenium.support.ui.Select I get an error message:
"No module named 'org'"
Would appreciate any help on this. I saw there was a similar question posted a few weeks ago but the link to that question is now broken. Thanks!
The path 'org.openqa.selenium.support.ui.Select' is a Java descriptor. In Python, make sure you have the Python Selenium module installed with pip install selenium, and then import it with import selenium.
For the Select function specifically, you can import that with the following
from selenium.webdriver.support.ui import Select
Then you'll be able to use it like this:
select = Select(b.find_element_by_id(....))

Trouble with Selenium Python related Imports

I'm going to make an application using the Selenium WebDriver (python) and I was having trouble with some of the initial setup. I want to use the page object design pattern so I've been trying to import some materials in this way:
from page_objects import PageObject, PageElementfrom page_objects import PageObject, PageElement
as can bee seen in the documentation here: https://page-objects.readthedocs.io/en/latest/tutorial.html#a-simple-page-object
I'm using PyCharm for development and I've updated selenium and added it to the project interpreter but this import statement still will not work for me. I'm not sure which external module this import relates to or if its just some part of selenium, so any information would be greatly appreciated. Thanks!
That readthedocs is for the page_objects library, the repo for that library can be found here. You need to install that library before you can import it

Categories