Python Selenium, find element by name is not working [duplicate] - python

This question already has answers here:
find_element_by_* commands are deprecated in Selenium
(4 answers)
Closed 7 months ago.
My code opens driver and opens website but gives error about:
AttributeError: 'WebDriver' object has no attribute 'find_element_by_name'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Firefox()
driver.get("https://www.python.org")
time.sleep(5)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
driver.close()

Use ID to find element
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Firefox()
driver.get("https://www.python.org")
time.sleep(5)
search_bar = driver.find_element(By.ID, "id-search-field")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
driver.close()

Related

Python Selenium for Web Scraping not working [duplicate]

This question already has answers here:
Error "name 'by' is not defined" using Python Selenium WebDriver
(3 answers)
Closed 3 months ago.
from selenium import webdriver
import os
os.environ['PATH'] += "C:\\Users\\czoca\\PycharmProjects\\pythonProject4\\chromedriver.exe"
driver = webdriver.Chrome()
driver.get("https://www.teintes.fr/p/recapitulatif.html")
driver.implicitly_wait(10)
myelement1 = driver.find_element(By.ID, "Autoriser")
myelement = driver.find_element(By.ID, "Carens III")
myelement1.click()
myelement.click()
This is giving me this error:
""Traceback (most recent call last):
File "C:\Users\czoca\PycharmProjects\imagesscraping\main.py", line 9, in
myelement1 = driver.find_element(By.ID, "Autoriser")
^^
NameError: name 'By' is not definedenter image description here
You are missing the import of 'By' from the package selenium. You can import it like so:
from selenium.webdriver.common.by import By
You can look up more details about selenium on the docs
Add the following import to your file:
from selenium.webdriver.common.by import By
And it should be good.
You need to import the By module from Selenium in order to use it ( I agree that it is not obvious at first time), by simply adding this line
from selenium.webdriver.common.by import By
You will also probably need thoses packages :
# Import required packages
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver import EdgeOptions
Hope I helped you
Have a good day

Element's Click Intercepted [duplicate]

This question already has answers here:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted error using Selenium and Java in headless mode
(8 answers)
Closed 1 year ago.
i've been trying to make selenium click a button for me but all i get is element click intercepted
i first thought about exception handling but i want to know if there's a better way
eitherway i am a bit confused
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from win10toast import ToastNotifier as Notifier
from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep
Path: str = "C:\\Users\\rolan\\Desktop\\chromedriver.exe"
Link: str = "https://www.roblox.com"
Notification = Notifier()
Notification.show_toast("hulks bot", "settings things up...")
sleep(1)
Option = Options()
Option.add_argument("start-maximized")
Browser = webdriver.Chrome(service = Service(Path), options = Option)
Browser.get(Link)
def Perform_Actions():
sleep(1)
Login_Button = Browser.find_element(By.ID, "main-login-button")
Login_Button.click()
Perform_Actions()```
much appreciated help
fixed all you needed to do was just click on the main container of everything(just the screen)

Python - How do I switch between tabs in selenium [duplicate]

This question already has answers here:
Selenium Switch Tabs
(4 answers)
Closed 2 years ago.
I'm new to python and selenium and I'm trying to switch between tabs, I've already tried using the normal key commands, ActionChains, and Keys.CONTROL, but it's not working, how would I do this. The program is the following
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import webbrowser
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import multiprocessing
driver1 = webdriver.Chrome(executable_path="C:\mydriver\chromedriver")
ar = ['https://google.com', 'https://bing.com']
for page1 in (ar):
driver1.execute_script(f"window.open ('{page1}')")
time.sleep(3)
for x in range (5):
driver1.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
take a look at this guide:
https://www.browserstack.com/guide/how-to-switch-tabs-in-selenium-python
But this is the key part:
#get current window handle
p = driver.current_window_handle
#get first child window
chwd = driver.window_handles
for w in chwd:
#switch focus to child window
if(w!=p):
driver.switch_to.window(w)
break
time.sleep(0.9)
print("Child window title: " + driver.title)

How to let webdriver.Firefox() open minimized? [duplicate]

This question already has answers here:
How to make Firefox headless programmatically in Selenium with Python?
(7 answers)
Closed 2 years ago.
I have this code:
from selenium import webdriver
driver = webdriver.Firefox()
But how can I let the browser open up minimized or hidden?
The headless property will do the task for you. Import Options() from selenium.webdriver.firefox.options
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
my_opt = Options()
my_opt.headless = True
driver = webdriver.Firefox(options=my_opt, executable_path=r'path_to_your_geckodriver')
driver.get("url_you_want_to_access")
All set!

Selenium code doesn't terminate

The following code won't terminate. What could be the reason for that?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.quora.com/physics")
elem = driver.find_element_by_class_name("cancel")
#ele=elem[0]
print "done"
Try Using linkText instead of className. It is working when i tried.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.quora.com/physics")
elem = driver.find_element_by_link_text("Close & Read Quora")
print "done"

Categories