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")
Related
I currently wish to select a value from a drop-down menu in python with selenium.
The site is the following: https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M
My current code
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
import time
driver = webdriver.Safari(executable_path = '/usr/bin/safaridriver')
wait = WebDriverWait(driver, 20)
driver.get("https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M")
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#class='form-group' and .//label[#for='programok']]//div[contains(#class,'css-1wy0on6')]"))).click()
time.sleep(1)
current_program = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#class,'css-26l3qy-menu')]")))
current_program.click()
My issue is that I am not able to find out the needed xpath code for the pop-upped drop-down menu, since I can see it in developer view only if the list is clicked, but I can see then only that "//div[contains(#class,'css-26l3qy-menu')]" is a suitable xpath for the menu. (see the enclosed picture)
But it chooses a random value. How specify which value I want to select?
I'm trying to click on a "VIEW" button from the table shown below in the image from this site using Selenium Python.
I tried using xpath, name and all. How to create a loop for this?
All the entries is to be left empty just click on search button a table is displayed from there "VIEW" button is to be selected
Code:
Check below lines , and change the path of driver
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
opt = webdriver.ChromeOptions()
opt.add_argument("--ignore-certificate-errors")
opt.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path="C:\\chrome driver\\chromedriver.exe", options=opt)
driver.get("http://rera.rajasthan.gov.in/ProjectSearch")
search_btn = driver.find_element_by_xpath('//*[#id="btn_SearchProjectSubmit"]')
# invoke the click() action
search_btn.click()
xpath_first_view = '//*[#id="OuterProjectGrid"]/div[3]/div[2]/div/table/tbody/tr[1]/td[7]'
# wait for the element to ensure it is available
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,xpath_first_view)))
time.sleep(1) # to ensure click is not very quick
view1_btn = driver.find_element_by_xpath(xpath_first_view).click()
I want to click first on the link of the table with text A218012216.
It seems that table/links code are hidden inside JS.
I tried several ways, but without success.
This is my code so far:
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
url0 ="https://ccrecordse.tarrantcounty.com/AssumedNames/SearchEntry.aspx"
driver = webdriver.Chrome(executable_path="D:\Python\chromedriver.exe")
driver.get(url0)
time.sleep(3)
#fill the form # select by visible text
selectStart = driver.find_element_by_id('x:11265151.0:mkr:3')
selectStart.send_keys('09/05/2019')
selectEnd = driver.find_element_by_id('x:1246303050.0:mkr:3')
selectEnd.send_keys('09/05/2019')
#submit the form
driver.find_element_by_id("cphNoMargin_SearchButtons2_btnSearch__5").click()
time.sleep(3)
driver.find_element_by_link_text('A218012216').click()
How can I get that information?
The problem is that you are trying to use find_element_by_link_text on an element that is not an <a> tag link_text is only for <a> tags...
In my solution, you will see I am using WebDriverWait this is best practice in selenium...
Also, I used the XPath locator with text()=the_text note that the the_text will need to change as the date changes (you are searching for the future date 09/05/2019 so it shows the current date therefor the text will change...)
The Solution:
import time
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
url0 ="https://ccrecordse.tarrantcounty.com/AssumedNames/SearchEntry.aspx"
driver = webdriver.Chrome(executable_path=r"D:\Python\chromedriver.exe")
driver.get(url0)
time.sleep(3)
#fill the form # select by visible text
selectStart = driver.find_element_by_id('x:11265151.0:mkr:3')
selectStart.send_keys('02/19/2019')
selectEnd = driver.find_element_by_id('x:1246303050.0:mkr:3')
selectEnd.send_keys('02/19/2019')
#submit the form
driver.find_element_by_id("cphNoMargin_SearchButtons2_btnSearch__5").click()
the_text = "A219002410"
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='"+the_text +"']"))).click()
Hope this helps you!
I'm trying to click on the drop down menu but with no luck .
the menu is activated by javascript .
I tried to click on the link inside the parent div but nothing happens
here is some code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.energisa.com.br/Paginas/login.aspx")
select_element = Select(driver.find_element_by_id('ddlEstado'))
select_element.select_by_value('MG')
# select by visible text
select_element.select_by_visible_text('MG')
As per the your question the website https://www.energisa.com.br/Paginas/login.aspx the dropdown menu is not with in a Select tag, so Select class won't work here.
Once the url is accessed, you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.energisa.com.br/Paginas/login.aspx")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#class='estado']/div[#class='select2-container' and #id='s2id_ddlEstado']"))).click()
driver.find_element_by_xpath("//ul[#class='select2-results' and #id='select2-results-1']//li/div[normalize-space()='MG']").click()
The dropdown you are trying to click is not actually a SELECT element so you can't use the Select class. The SELECT you are trying to click is just a backing element but it's invisible so you can't interact with it.
To make this work, you will need to click the dropdown element to expose the options and then click the desired option.
driver.find_element_by_css_selector("#s2id_ddlEstado > a").click()
driver.find_element_by_xpath("//ul[#id='select2-results-1']/li[.='MG']").click()
This is untested code, so you may need to add a wait...
The one which appears like a select_list is not a select_list, the purpose of this kind of select_list is, we can write into the text_field to pick up the elements from the huge list, if you type 'M', then all the options which has M will be displayed.
Write the following code, it would work.
WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.ID,"loadingContent")))
driver.find_element_by_id("s2id_ddlEstado").click
driver.find_element_by_xpath("//ul[#id='select2-results-1']//div[text()='MG']").click
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