Selenium Python options click - python

I want help in a little thing,
Look this:
I want to press the option named Boleto Bancario, but look the html
Than how I will press the second option with selenium PYTHON

Please Check the snippet.
You can select by value
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome(r'chromedriver.exe')
driver.get('url')
sct = Select(driver.find_element_by_id('idFormaPagamento'))
sct.select_by_value('813640')
You can select by index
sct.select_by_index('1')

You can select the value in any dropdown by three different ways:
select_by_value()
select_by_index()
select_by_visible_text()
So you may simply go and choose the option like this:
select_by_value('813640')
select_by_index('1')
select_by_visible_text('Boleto Bancario')
Hope this works for you.

Related

select country with python selenium

Click here for see the codeI'm learning python, but I don't know how to select this part, anyone help me?
I tried to select the cell with xpath and put the country but I don't know how to select it to assign the value
test = driver.find_element_by_xpath("//input[#type='search']")
test.send_keys('United States')
test.click()
What you want to do is to select the list item.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Firefox()
driver.get('url')
select = Select(driver.find_element_by_xpath("//input[#type='search']"))
# select by visible text
select.select_by_visible_text('United States')
# select by value
select.select_by_value('3')
Link to original answer](How to select a drop-down menu value with Selenium using Python?).
Try this.
# Assuming this this the correct xpath to the search bar
test = driver.find_element_by_xpath("//input[#type='search']")
test.click()
driver.find_elements_by_xpath("//li[contains(text(), 'United States') and #class='up-menu-item']").click()

Select Element in Python Selenium for Dropdown Menu

I am new to Python and I am trying to use Selenium to select a value from a drop down menu on a Firefox browser.
This is what is am trying. Please let me know what I am doing wrong:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
browser = webdriver.Firefox()
browser.get('URL')
select = Select(browser.find_element_by_id('Yesterday'))
I need to add the following to my original post:
I have used Selenium IDE to try and identify the selector. Please image below. I would like to select "Yesterday" from the drop down list.
enter image description here
You can use like this, locate Select drop down then select the Custom value
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_name('locator of the select'))
select.select_by_value('Custom')
Also, you can use the index
select = Select(driver.find_element_by_name('locator of the select'))
select.select_by_index(6)
You can use browser.find_element_by_link_text("Yesterday") in place of browser.find_element_by_id. Since it does not contain an id tag it will not work.

Selenium Stops Extracting Option Text After Click Command

I am trying to use Selenium to extract dynamically loaded content. The content is on http://www.afl.com.au/stats
I am attempting to navigate to the 'Players' tab, then obtain a list of all the Seasons available. When I do this from the Teams tab, the following code works:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome(executable_path=r'D:\ChromeDriver\chromedriver.exe')
driver.get('http://www.afl.com.au/stats')
dropdown_menu = Select(driver.find_element_by_xpath('//*[#id="selTeamSeason"]'))
for option in dropdown_menu.options:
print(option.text)
which gives me a list of all the options available in the Seasons tab.
However, when I click to the 'Players' tab first, I am unable to get the same list with almost identical code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Chrome(executable_path=r'D:\ChromeDriver\chromedriver.exe')
driver.get('http://www.afl.com.au/stats')
driver.find_element_by_xpath('//*[#id="stats_tab"]/ul/li[2]').click()
time.sleep(3)
dropdown_menu = Select(driver.find_element_by_xpath('//*[#id="selTeamSeason"]'))
for option in dropdown_menu.options:
print(option.text)
The click successfully executes, I wait for the content to update, but instead of printing all the years (2001 to 2018), Selenium prints 18 instances of empty strings. I am thoroughly stumped. Any help at all would be appreciated.
In your first case , locator(//*[#id="selTeamSeason"]) is pointing to season dropdown of Teams tab and page has only one matching node at the time ,so its working for you.
But in the second case , for the same locator there are 2 matching nodes are available and in this case selenium automatically pick the first one(Its an hidden element in your case).
So try to build a unique xpath with can work in both the tabs.
You can try //div[#id='stats-player-stats']//select[#id='selTeamSeason'] locator for season dropdown in Players tab and //div[#id='stats-team-stats']//select[#id='selTeamSeason'] for season dropdown in Teams tab
Hope this will work for you
Instead of using Select just find the xpath and get all the option tags like below, tested and works.
element = driver.find_element_by_xpath('//*[#id="selTeamSeason"]')
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
print(option.text)
In your first attempt the following Locator Strategy worked on the default TEAMS TAB :
dropdown_menu = Select(driver.find_element_by_xpath('//*[#id="selTeamSeason"]'))
As the first match as per the Locator Strategy was the Dropdown itself which is not the case when dealing with PLAYERS TAB. On PLAYERS TAB to list of all the options available in the Seasons Dropdown you can use the following code block :
dropdown_menu = Select(driver.find_element_by_xpath("//div[#id='stats-player-stats']//select[#id='selTeamSeason']"))
for option in dropdown_menu.options:
print(option.text)

python selenium - picking the right option with the right name

https://gyazo.com/b3e792b66775f64147671a6f23bd52c7
On the picture out from the first red dot, is there a list of options if u click on the "vælg" button (its in my language). I want my script to click on the right option.
Lets say the right one is "unisexur." How do I make my script click the option that says "unisexur" in the option list.
I know how to choose one of the options by e.g. xpath:
choice = browser.find_element_by_xpath('//*[#id="matrix-element-666"]/option[2]')
sleep(1)
choice.click()
which will make the code take the second option. So that is not what I want.
The html code is in the picture in the top..
You are probably looking for select_by_value.
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('matrix-element-666'))
select.select_by_value('unisexur').click()

Selenium - send keys - what element should I use

I'm trying to scroll at the bottom of the page. I was adviced, here on SO, to do this:
from selenium.webdriver.common.keys import Keys
element = driver.find_element_by_ ...
element.send_keys(Keys.CONTROL , Keys.END)
I can't figure out what element shoul I use. I was trying to put a webdriver instance instead of element but it did not work. I need something like current window element?
Have you any ideas?
This should be enough to make scroll to page bottom
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome()
driver.get("site_name")
driver.find_element_by_xpath('//body').send_keys(Keys.CONTROL+Keys.END)
A simple javascript should be sufficient as well. Python syntax
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
body = driver.find_element_by_xpath('/html/body')
body.click()
ActionChains(driver).key_down(Keys.COMMAND).send_keys(Keys.ARROW_DOWN).perform()
Looking at your previous question Link This works on mac. Change the combo for Windows.

Categories

Resources