Error "name 'by' is not defined" using Python Selenium WebDriver - python

I keep getting an error as below:
NameError: name 'By' is not defined
for the code
chrome_driver_path = r"C:\chromedriver.exe"
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Chrome(chrome_driver_path)
browser.delete_all_cookies()
browser.get("https://www.google.com/")
wait = WebDriverWait(browser, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="tsf"]/div[2]/div[3]/center/input[1]')))
Is my import correct?

You have to import it:
from selenium.webdriver.common.by import By

You can import By by using:
from selenium.webdriver.common.by import By

Adding this line at the top of the code resolved my problem:
from selenium.webdriver.common.by import By

Related

How to upload file using Selenium with Python

I want to attach file through below link. How can I do that using selenium python?
My code:
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome(executable_path="C:\Program Files (x86)\Drivers\chromedriver.exe")
driver.get("https://www.tascoutsourcing.com/job-openings/quality-assurance-engineer/")
driver.maximize_window()
time.sleep(10)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
driver.find_element_by_xpath("//*[#id='content']/div[2]/div[2]/div/div/div/div[1]/div/div/div/button").click()
driver.find_element_by_xpath("//*[#id='content']/div[2]/div[2]/div/div/div/section/div/form/div[3]/div[3]/div").click()
paul, please try to run below code - I have ran the same code in my system and it is working for me.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
driver.get("https://www.tascoutsourcing.com/job-openings/quality-assurance-engineer/")
# Click on Apply Button
Apply_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Apply Now']/parent::button")))
action.move_to_element(Apply_btn).click().perform()
time.sleep(2)
Upload_File = driver.find_element_by_xpath('/html/body/label/input').send_keys("YourFilePath")
Do let me know if it shows error.

why i am not getting chromedriver exception in python

I am using the following code.
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#login
driver = webdriver.Chrome()
driver.get("url")
#time.sleep(10)
driver.set_window_size(920, 680)
driver.find_element(By.ID, "loginUserName").send_keys("xx")
driver.find_element(By.ID, "loginPassword").send_keys("yy")
driver.find_element(By.ID, "loginButton").click()
It is working fine for me and i'm not included any path and set property for chrome-extension. If i share this program,the opposite person is getting the following error and they have chromedriver.exe in python folder and in that same folder they created this python file
Traceback (most recent call last): File
"C:\Users\1025043\AppData\Local\Programs\Python\Python37-32\anu\1.py",
line 8, in
driver = webdriver.Chrome(chrome_driver_path) NameError: name 'chrome_driver_path' is not defined
In my path i am having,
['',
'C:\Users\1024983\AppData\Local\Programs\Python\Python37\python37.zip',
'C:\Users\1024983\AppData\Local\Programs\Python\Python37\DLLs',
'C:\Users\1024983\AppData\Local\Programs\Python\Python37\lib',
'C:\Users\1024983\AppData\Local\Programs\Python\Python37',
'C:\Users\1024983\AppData\Local\Programs\Python\Python37\lib\site-packages',
'C:\Users\1024983\AppData\Local\Programs\Python\Python37\lib\site-packages\win32',
'C:\Users\1024983\AppData\Local\Programs\Python\Python37\lib\site-packages\win32\lib',
'C:\Users\1024983\AppData\Local\Programs\Python\Python37\lib\site-packages\Pythonwin']
What is the reason for this.?
Note: I am using Windows 10, Python 3.7.4 ,selenium 3.141.0 ,Chrome Version 78.0.3904.108(opposite person also using this version only)
The chromedriver.exe should be included in the executable PATH.
Download the chromedriver.exe and add this to the code as below. (add chrome driver path as it's located in your machine) or you can include in to the PATH variable in environment variables in windows 10.
E.g.
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#login
driver = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')
driver.get("url")
#time.sleep(10)
driver.set_window_size(920, 680)
driver.find_element(By.ID, "loginUserName").send_keys("xx")
driver.find_element(By.ID, "loginPassword").send_keys("yy")
driver.find_element(By.ID, "loginButton").click()

Unable to locate element selenium Python

I am trying to fill in a form using this code:
from selenium import webdriver
import time
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome( chrome_options=chrome_options)
browser.get('https://www.vwe.nl/Autobedrijf/In-en-verkoop/bpm-calculator-
doorverkoop')
time.sleep(5)
#browser.switch_to_frame(browser.find_element_by_id("_hjRemoteVarsFrame"))
kenteken = browser.find_element_by_id('InputControl_txtKenteken')
kenteken.send_keys('simple text')
I tried to see if there is an iframe but it didn't work.
But it drops an error : Unable to locate element
Your element inside iframe, you need switch it first.
This is the iframe locator: By.CLASS_NAME -> iframe_bpm.
You can use combination WebDriverWait and expected_conditions then utilize frame_to_be_available_and_switch_to_it method to switch.
browser = webdriver.Chrome( chrome_options=chrome_options)
browser.get('https://www.vwe.nl/Autobedrijf/In-en-verkoop/bpm-calculator-doorverkoop')
browser.maximize_window()
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, 'iframe_bpm')))
kenteken = browser.find_element_by_id('InputControl_txtKenteken')
kenteken.send_keys('simple text')
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
There is an iframe you need to switch it first.Use WebDriverWait.
driver.get("https://www.vwe.nl/Autobedrijf/In-en-verkoop/bpm-calculator-doorverkoop")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,'.iframe_bpm')))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"InputControl_txtKenteken"))).send_keys('text')
use following imports.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

cannot print out specific data with xpath and webdriver

cannot print out specific data with xpath and webdriver
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://datatables.net/")
print driver.findtext('.//*[#id="example"]/tbody/tr[1]/td[1]')
Expected Result:
Airi Satou
Try this one to get desired output:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
print(wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//table[#id="example"]//td'))).text)

Python/Selenium im getting error and can not fix it

here is my code
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import random
results = []
with open("al.txt") as inputfile:
for line in inputfile:
results.extend(line.strip().split(','))
x = random.choice(results)
driver = webdriver.Firefox()
while True:
driver.get(x)
element = driver.find_element_by_id("vB_Editor_QR_textarea")
driver.implicitly_wait(10)
element.send_keys("example")
driver.implicitly_wait(60)
driver.close()
i get
IndentationError: unexpected indent
PS C:\Users\asus\temp> python clickex1.py
File "clickex1.py", line 24
driver.implicitly_wait(60)
is it possible that selenium is installed wrong so python doesn't read the library?
the other lines are working perfectly fine. i need some help with this im new in using selenium and also python,
the script needs to go to the random websites in al.txt and post something in the designated area

Categories

Resources