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.
Related
I have this code :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver import EdgeOptions
import os
os.environ['PATH'] += "C:\\Users\\czoca\\PycharmProjects\\pythonProject4\\chromedriver.exe"
driver = webdriver.Chrome()
driver.get("https://www.teintes.fr/")
driver.implicitly_wait(10)
myelement = driver.find_element(By.XPATH, "/html/body/div[6]/div[2]/div[1]/div[2]/div[2]/button[1]/p")
myelement = driver.find_element(By.NAME, "Carens III")
myelement1.click()
myelement.click()
Everything seems ok I am following some tutorials and documentation, for the XPATH and I tried other attributes..
But I have a pop up to consent to the web terms that i have to press Autorizate. But it wont click on it. Any ideia why?enter image description here
It is the button "Autoriser"
You can try this:
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
driver = webdriver.Chrome()
url = 'https://www.teintes.fr/'
driver.get(url)
accept_cookies_button = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[text()='Autoriser']")))
accept_cookies_button.click()
Advices:
Use recognizables selectors
It is a good tip to wait for the element you will use
I'm trying to login with seleinum automatically. I've used
driver.execute_script
driver.find_element_by_css_selector
driver.find_element_by_xpath
.
from selenium import webdriver
from webdriver_manager import chrome
driver = webdriver.Chrome(chrome.ChromeDriverManager().install())
driver.get("https://naco999.com/")
driver.find_element_by_css_selector('#login_id').send_keys("id")
driver.find_element_by_css_selector("#login_pw").send_keys("pw")
But none of these seems to work. How can I?
There are 3 elements matching #login_id css_selector.
Try using this:
driver.find_element_by_css_selector(".header-one #login_id").send_keys("id")
driver.find_element_by_css_selector(".header-one #login_pw").send_keys("pw")
Also, you should add a wait to send the text when the elements are loaded.
Like this:
from selenium import webdriver
from webdriver_manager import chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(chrome.ChromeDriverManager().install())
wait = WebDriverWait(driver, 20)
driver.get("https://naco999.com/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".header-one #login_id"))).send_keys("id")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".header-one #login_pw"))).send_keys("pw")
The problem is that as the browser window open the Facebook page I expect it to click on the Create New Account .
The XPATH for Create New Account that I used is
//a[text()='Create New Account']
but it open's facebook.com then suddenly closes although I have other scripts working properly.
The complete Code :
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://facebook.com")
link_locator = '//a[text()="Create New Account"]'
create_account = Wait(driver, 5).until(EC.presence_of_element_located((By.XPATH, link_locator))).click()
Browser has been closed immediately, coz there was just command click, driver made it and due to nothing next, closed the driver.
Try to add time.sleep(10) at the end, and you will see that signup pop appeared:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://facebook.com")
link_locator = '//a[text()="Create New Account"]'
create_account = Wait(driver, 5).until(EC.presence_of_element_located((By.XPATH, link_locator))).click()
time.sleep(10)
driver.quit()
I'm having a problem clicking the Radio button for the Registered Projects on this site. It is not clicking with my code in selenium webdriver.
import urllib.request
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
url = 'https://maharerait.mahaonline.gov.in'
chrome_path = r'C:/Users/User/AppData/Local/Programs/Python/Python36/Scripts/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_path)
driver.implicitly_wait(10)
driver.get(url)
soup=BeautifulSoup(driver.page_source, 'lxml')
link =driver.find_element_by_link_text("Search Project Details")
link.click()
driver.find_element_by_id("Promoter").click()
Use WebDriverWait and java Scripts Executor to click on the Registered Project radio button.
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
url = 'https://maharerait.mahaonline.gov.in'
chrome_path = r'C:/Users/User/AppData/Local/Programs/Python/Python36/Scripts/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_path)
driver.get(url)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='search-pro-details']//a[contains(.,'Search Project Details')]"))).click()
Registered_Project_radio= WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"Promoter")))
driver.execute_script("arguments[0].click();",Registered_Project_radio)
Browser snapshot:
WebDriverWait - An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code.
import urllib.request
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://maharerait.mahaonline.gov.in'
chrome_path = r'C:/Users/User/AppData/Local/Programs/Python/Python36/Scripts/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_path)
driver.get(url)
links = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME,\
"search-pro-details")))
#Click on Search Project Details link
links.find_element_by_link_text("Search Project Details").click()
promoter_radio_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,\
"Promoter")))
#select radio button
promoter_radio_button.send_keys(Keys.SPACE)
Try using this:
driver.get('https://maharerait.mahaonline.gov.in')
link =driver.find_element_by_link_text("Search Project Details")
link.click()
time.sleep(2)
radio_btn = driver.find_element_by_id("Promoter")
radio_btn.click()
time.sleep(5)
driver.close()
I'm trying to learn Selenium webdriver using Python. My code for test case looks:
import unittest
#import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
#from selenium.webdriver.common.action_chains import ActionChains
class FindRouteToBerlin(unittest.TestCase):
# Start Firefox
def setUp(self):
self.driver = webdriver.Firefox()
def test_find_route_to_berlin(self):
driver = self.driver
# Open HERE Maps
driver.get('http://wego.here.com')
self.assertIn("HERE WeGo", driver.title)
# Go to Search field and fill with city
search = driver.find_element_by_xpath('//*[#id="searchbar"]/div/div/input')
search.send_keys('Berlin')
search.send_keys(Keys.RETURN)
# Press "Arrow"
arrow = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div[6]/div/div/div[1]/div[2]/button')))
arrow.click()
# Enter address
address = driver.find_element_by_xpath('//*[#id="itinerary_item_input_0"]')
address.send_keys('Szczecin')
address.send_keys(Keys.RETURN)
"""actions = ActionChains(driver)
actions.move_to_element(address)
actions.send_keys(Keys.RETURN)
actions.perform()"""
#def tearDown(self):
# self.driver.quit()
if __name__ == "__main__":
unittest.main()
First step with filling text "Berlin" and works without any problem. But when the second step comes it looks like Keys.RETURN is not executed after input "Szczecin".
I was trying to solve this problem with some ActionChains but after .perform() I have error and I've read that those problems are related to bugs in Selenium/Mozilla.
My code for ActionChains was:
actions = ActionChains(driver)
actions.move_to_element(address)
actions.send_keys(Keys.RETURN)
actions.perform()
Error: Message: POST
/session/1caed99c-1577-4f1d-804b-4ee397d8750b/moveto did not match a
known command
Any suggestions how can I solve this problem ?
You can try to select first element from drop-down suggestions instead of using Keys.RETURN:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
address = driver.find_element_by_xpath('//*[#id="itinerary_item_input_0"]')
address.send_keys('Szczecin')
first_suggestion = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[contains(#class, 'dropdown_list_item')]")))
first_suggestion.click()
Following is working for me:
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
def setUp():
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
base_url = ('enter your url')
driver.get(base_url)
task = driver.find_element_by_id("some_element_id")
task.send_keys("Some_text",Keys.ENTER)
setUp()