How to enter file path? - python

How can I do to type something in the field of the image below?
I've tried without success:
from threading import local
import pandas as pd
import pyautogui
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait.until(EC.presence_of_element_located((By.XPATH,"//input[#type='file']"))send_keys("C:/Users/my_user/Downloads/doch.jpeg")
for index, row in df.iterrows():
actions.send_keys((row["message"]))
actions.perform()
The only palliative solution was:
pyautogui.write((row["photo"]))
pyautogui.press("enter")
I don't want to use pyautogui as it uses the keyboard command and I can't do anything on the computer while the code is running.

Selenium can't upload files using the Windows select file option, so you'll have to do something else - you might be able to use the send_keys function, i.e.:
elem = driver.find_element(By.XPATH, "//input[#type='file']")
elem.send_keys('C:\\Path\\To\\File')
Note that this may not work, depending on the type of input, and you may be able to instead simulate a drag-and-drop operation if the website supports this.
See How to upload file ( picture ) with selenium, python for more info

For windows path you need double backslashes. Try this:
wait.until(EC.presence_of_element_located((By.XPATH,"//input[#type='file']"))send_keys("C:\\Users\\my_user\\Downloads\\doch.jpeg")

Related

Unable to import selenium webdriver from within a function

I have a few scripts, wherein I am required to import a few modules everytime. To avoid using the import statements everytime I write a new script, I tried to write a function as follows so that I can import the function instead. Here's how wrote the code for that :
def mylibs():
import selenium
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
return
mylibs()
But when I am running the next line of the code- which should use the webdriver imported above to launch an instance of the chrome browser :
browser = webdriver.Chrome(r"c:\users\nila9\drivers\chromedriver.exe")
I am getting an error like "webdriver not defined", so the browser fails to launch.
I am not able to understand what I am getting wrong here...I also tried to do it without the return also, but same result.
If this works, I can then import the module into any other script, whenever I need to use the block of import codes.
Any help appreciated.
try
driver = webdriver.Chrome(executable_path="C:\Users\nila9\drivers\chromedriver.exe")
or
driver = webdriver.Chrome("C:\Users\nila9\drivers\chromedriver.exe")

Creating a GUI for Selenium

I want to create a GUI for this code that automatically downloads PDF's. But I am not sure where to start. These are 2 inputs I need to change.
drp.select_by_visible_text('**Dan Pitts**')
checkfield=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="FieldCheckBox-**701847**"]')))
So just 2 input boxes and then have a submit button for it to open chrome and start.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
import time
driver=webdriver.Chrome()
driver.get('')
username=driver.find_element_by_xpath('//*[#id="username"]')
username.send_keys('')
password=driver.find_element_by_xpath('//*[#id="password"]')
password.send_keys('')
login=driver.find_element_by_xpath('/html/body/pcs-root/pcs-site-wrapper/div/div/div/div/pcs- login/div/form/button')
login.click()
time.sleep(10)
driver.switch_to.frame('pcsIFrame')
growerlist=driver.find_element_by_xpath('//*[#id="GrowerDropDownList"]')
drp=Select(growerlist)
drp.select_by_visible_text('**Dan Pitts**')
wait=WebDriverWait(driver,30)
getfields=driver.find_element_by_xpath('//*[#id="GetFieldsButton"]')
getfields.click()
##time.sleep(4)
checkfield=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="FieldCheckBox-**701847**"]')))
checkfield.click()
I have put the two input boxes in and it does work for the Grower. I cant seem to get it to work with xpath though.

Cannot select more dropdowns with Selenium Python

I am trying to scrape for car prices from this website:
To get car prices, you should fill out the form and I have to choose from dropdowns using Selenium.
I am using this code to choose from dropdowns:
# Imports
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
year_dropdown = Select(WebDriverWait(driver, 5)
.until(EC.element_to_be_clickable((By.ID, "j_id_3q-carInfoForm-year-selectOneMenu"))))
year_dropdown.select_by_value('2015')
But after I chose the year, it just keeps loading and never stops:
Any suggestions please?
I resolved the issue by using a real chrome driver. I was using chromdriver-manager package and when I removed it and downloaded a real chrome driver, the issue was gone.

Stop selenium/itertools from skipping over rows inputted from an excel spreadsheet

I have written to automate some work using selenium and python. The code uses a for-loop and gets its input from an excel spreadsheet and steps through the different screens of the web application and after the steps are complete goes to the next line in spreadsheet and so on and so forth. The issue I'm encountering is that the code skips through a few rows of the spreadsheet at a time randomly. How can I stop this from happening? Thanks. Below is my code.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import pdb
from itertools import islice
file_path = "file.xlsx"
driver = webdriver.Chrome(executable_path = "chromedriver.exe")
driver.get("url")
pdb.set_trace()
for index, row in islice(df.iterrows(), 0, None) :
driver.get("url")
driver.implicitly_wait(60)
driver.find_element_by_id("xyz").send_keys("abc")
.
.
.
driver.find_element_by_name("Submit").click()

Each time I run Selenium to ping a specific site, my WebDriver ends up going to a completely different URL

I want to go to this site: https://www.pluginillinois.org/offers.aspx?said=2
Each time I have some code that looks like the following:
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.common.exceptions import NoSuchElementException
import time
import pyautogui
driverpath = r"C:\Users\chromedriver.exe"
browserProfile = webdriver.ChromeOptions()
browserProfile.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
browser = webdriver.Chrome(driverpath, options=browserProfile)
browser.get('https://www.pluginillinois.org/offers.aspx?said=2')
My Selenium browser will open and go to a completely different URL instead. The browser will instead go to https://www.pluginillinois.org/OffersBegin.aspx.
Is there a way to obfuscate this behavior?

Categories