This is my first selenium project and I am trying to submit this form:
https://docs.google.com/forms/d/e/1FAIpQLSdwD1y2eBOoJ-hvk97wDGptfI9oYga8SqtUz7u3nrFbWM7hxw/viewform
I succeeded clicking Multiple choice and Checkboxes but I cant select the Drop down. I tried following with no success. All pointers are welcome:
import os
import requests
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://docs.google.com/forms/d/e/1FAIpQLSdwD1y2eBOoJ-hvk97wDGptfI9oYga8SqtUz7u3nrFbWM7hxw/viewform")
multiFirst = driver.find_element_by_xpath("//*[#id=\"i5\"]/div[3]/div")
multiFirst.click()
checkBocSecond = driver.find_element_by_xpath("//*[#id=\"i22\"]/div[2]")
checkBocSecond.click()
dropdownThird = driver.find_element_by_xpath("//*[#id=\"mG61Hd\"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[2]/div[5]")
ActionChains(driver).move_to_element(dropdownThird).click(dropdownThird).perform()
dropdownThird = driver.find_element_by_xpath(
'(//span[#class="quantumWizMenuPaperselectContent exportContent"])[1]')
dropdownThird.click()
WebDriverWait(driver, 5000).until(EC.presence_of_element_located(
(By.XPATH, '(//*[#jscontroller="liFoG"]//*[contains(text(),"Option 1")])[2]'))).click()
your locator is incorrect use above locator
you have to import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Selenium provides Select method for selecting drop downs.
from selenium.webdriver.support.select import Select
selection =Select(driver.find_element_by_xpath("yourXPATH"))
3 ways to select
select_by_index
selection.select_by_index(yourIndex)
It selects by index starting from 0 to end of your drop-down.
select_by_visible_text
selection.select_by_visible_text('yourText')
It uses the text inside the drop down.
select_by_value
selection.select_by_value('Value')
It uses the value attribute of drop-down list.
Related
I am unable to retrieve any search results in fbref.com when using either of send_keys and execute_script in selenium for python using chrome web driver
This is the code ive used so far:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
import csv
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.get("https://fbref.com/en/")
element = driver.find_element(by=By.CLASS_NAME, value="ac-hint")
action = ActionChains(driver)
element1= driver.find_element(by=By.CSS_SELECTOR, value=("input[type='search']"))
action.click(on_element=element1)
action.perform()
#element.send_keys("lionel messi")
#driver.execute_script("arguments[0].value='lionel messi'",element)
element2=driver.find_element(by=By.CSS_SELECTOR, value=("input[type='submit']"))
action.click(on_element=element2)
action.perform()```
The code is able to interact with the search button and the text is typed and the search button is clicked without any trouble but the search result is as follows:
which basically means that the search was invalid ,ive tried to search manually in the browser window opened by the driver and that gives me a successful result
You are doing your player name input in the wrong field, if you look closely at the html, there are 2 input fields for the search.
instead of the "ac-hint", use "ac-input":
element = driver.find_element(by=By.CLASS_NAME, value="ac-input")
The locator strategy you have used to identify the search field
doesn't identifies the desired element uniquely within the HTML DOM
Solution
To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solution:
Code Block:
driver.get("https://fbref.com/en/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='search'][placeholder='Enter Person, Team, Section, etc']"))).send_keys("lionel messi" + Keys.RETURN)
Note: You have to add the following imports :
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
Browser Snapshot:
I am new to python and selenium. Trying to automate download of files from the dropdown menu on the below link for the first time. The code gets stuck after page loads i.e the drop down does not work and gives me an error. Sorry if my code looks clunky. Any help is appreciated.
Thanks
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import time
s=Service("C:\\python\\chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get('http://www.amfiindia.com/research-information/aum-data/classified-average-aum')
#monthname=driver.find_element(By.XPATH,"//*[#id='AaumDate']")
monthdd=Select(monthname)
monthdd.select_by_value('01-Jan-22')
time.sleep(3)
typename=driver.find_element(By.XPATH,"//*[#id='AumType']")
typedd=Select(typename)
typedd.select_by_visible_text('Scheme category wise')
time.sleep(3)
mfname=driver.find_element(By.XPATH,"//*[#id='AumMFName']")
mfdd=Select(mfname)
mfdd.select_by_visible_text('All')
time.sleep(3)
driver.find_element("class name","sprite-inter go-btn").click()
wait=WebDriverWait(driver,20)
driver.get('http://www.amfiindia.com/research-information/aum-data/classified-average-aum')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#divAumPeriod > span > a > span.ui-button-text"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[#class='ui-menu-item']/a[.='January - 2022']"))).click()
Most of these tags open like so and have another list with them that's not using the select tag.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm trying to select 'Newest' from the drop-down menu.
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
# options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=options)
url = 'https://play.google.com/store/apps/details?id=com.whatsapp&hl=en&showAllReviews=true'
driver.get(url)
state_selection = driver.find_element_by_xpath("//div[.='%s']" % "Most relevant")
state_selection.click()
state_selection.send_keys(Keys.UP)
state_selection.send_keys(Keys.UP)
state_selection2 = driver.find_element_by_xpath("//div[.='%s']" % "Newest")
state_selection2.send_keys(Keys.RETURN)
but as soon as it reaches Newest and as I send command to press enter(as shown in code),it resets to "Most Relevent". I'm not able to get my head around on how to achieve this.
After you have clicked state_selection, something like this will click "Newest":
driver.find_element_by_xpath("//div[#role='option']/span[contains(text(),'Newest')]").click()
The more robust method would be working with WebdriverWait to allow the DOM to update, so:
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.XPATH, "//div[#role='option']/span[contains(text(),'Newest')]"))).click()
Note you need these imports for WebdriverWait:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
There are different ways to find
Index
Value
Visible Text
When you use the xpath if the values are changed in future,it pick that element present in that location only.So its better to user select by visible text
state_selection=Select(driver.find_element_by_xpath("//div[.='%s']" % "Most relevant").click();
state_selection.select_by_visible_text("Dropdown Visible Text")
The code I have opens a website and using actionChains, it right clicks on the desktop which brings up a menu. I now need to do 3 more things with actionChains. I need to hover over the item that says Save Page WE and then click an item on the sub-menu that pops up and then hit the enter button. Can anyone show me how to do this? Thanks
from selenium import webdriver
from selenium.webdriver import ActionChains
fp = webdriver.FirefoxProfile('/Users/Jake/AppData/Roaming/Mozilla/Firefox/Profiles/emjx467y.default-1524932841911')
driver = webdriv[enter link description here][1]er.Firefox(fp)
driver.get('http://www.tradingview.com/screener')
element = driver.find_element_by_link_text('Screener')
actionChains = ActionChains(driver)
actionChains.context_click(element).perform()
By using this line : actionChains.context_click(element).perform() , you are trying to right click on Screener menu. But the ideal behavior should be to be hover on it and select one options out of 3.
I'm selecting Forex Screener, you can select any one as per your requirement.
For hover over you can try this code :
actionChains.move_to_element(element).perform()
Full code would be like this :
driver.get("http://www.tradingview.com/screener")
wait = WebDriverWait(driver,40)
driver.find_element_by_css_selector("span[class*='tv-header__dropdown-wrap--noarrow'] span[class$='lang-icon--current-lang']").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='English (UK)']/parent::a"))).click()
element = driver.find_element_by_link_text('Screener')
actionChains = ActionChains(driver)
actionChains.move_to_element(element).perform()
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Forex Screener"))).click()
Make sure to import these :
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.webdriver.common.action_chains import ActionChains
I am attempting to make a auto script bot that find a specific item and adds the item to the users cart and so forth. Right now I am caught up getting python to select the size from the dropdown menu.
I also went with the WebDriverWait function because it was giving me a element not found error , so i assumed it the 'size' element had not loaded yet.
Python is also throwing me this error
"TypeError: 'str' object is not callable"
Below is also a picture of the html code I am referencing to pull the information from. Also will appreciate any advice on better executions.
from selenium import webdriver
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.common.by import By
# Open chrome web browswer and directs to supreme.com
browser = webdriver.Chrome()
browser.get('http://www.supremenewyork.com/shop/all')
#Find specific item
browser.find_element_by_xpath('//*
[#id="container"]/article[14]/div/a/img').click()
#Wait for element to load
pause = WebDriverWait(browser,10).until(
EC.visibility_of_any_elements_located(By.ID('size'))
)
# Select size
Select = Select(browser.find_element_by_id('size'))
Select.select_by_visible_text("Large")
Try below code.
from selenium import webdriver
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.common.by import By
browser = webdriver.Chrome()
browser.get('http://www.supremenewyork.com/shop/all')
browser.find_element_by_xpath('//*[#id="container"]/article[12]/div/a/img').click()
WebDriverWait(browser,10).until(EC.visibility_of_any_elements_located((By.ID,'size')))
select = Select(browser.find_element_by_id('size'))
select.select_by_visible_text("Medium")