Select dropdown option with Selenium (Python) - python

I'm kinda new at Selenium, so a proposed myself a project. I'm trying to get as much information as I can from this URL https://statusinvest.com.br/acoes/proventos/ibovespa
Until that time I was able to do everything, EXCEPT change the default option at the "Filtro por Índice". I would like to change it from "Ibovespa" to "--GERAL--" but it has been harder than I would expect! I tried via classical XPath (find then click) and by the Select() class in Selenium, but it appears to be beyound my knowledge and I'm totally stuck...
Anyone has any tip on how to accomplish it?
Thanks!

So a very simple way to change the input option would be to do:
from selenium.webdriver.common.by import By
select_obj = driver.find_element(By.CLASS_NAME, 'select-wrapper') # object that contains all of the elements for first input selector
select_obj.find_element(By.TAG_NAME, 'input').click() # click the input object to bring up the options
select_obj.find_elements(By.TAG_NAME, 'li')[0].click() # click the first option

Related

Is there any way to click on "plain text" using selenium?

Apologies if this question was answered before, I want to click on an area in a browser with plain text using Selenium Webdriver in python
The code I'm using is:
element_plainText = driver.find_elements(By.XPATH, '//*[contains(#class, "WgFkxc")]')
element_plainText.click()
However this is returning "ElementNotInteractableException". Can anyone help me out with this?
Selenium is trying to be helpful here, by telling you why it won't click on the element; ElementNotInteractableException means it thinks that what you're trying to click on isn't clickable.
This usually happens because either:
The element isn't actually visible, or is disabled
Another element is "overlapping" the element, possibly invisibly
You're clicking something Selenium thinks won't do anything, like plain text
There's two things I'd try to get around this. Firstly, Actions. Selenium has an Action API you can use to cause specific UI events to occur. I'd suggest finding the co-ordinates of the text, then making Selenium click those co-ordinates instead of telling it to click the element. Read more about that API here.
Secondly, try clicking it with Javascript, using a Javascript Executor. That can often give you the same outcome as using Selenium directly, without it being so "helpful".

how can I get the value in this page in python selenium

I am trying to get the value from webpage using selenium base on python. However, I do not get the point about how to make it. The value I want to catch is in the red mark side of below picture. Please help me to figure out this. Thank you very much!
Please click here to check the picture
There are few ways (By css,by xpath...) I recommend you reading this page: http://selenium-python.readthedocs.io/locating-elements.html
You also need some understanding about html, css and the DOM but the simplest way to get the value you want is by xpath (But its the slowest)
Open your web browser lets say chrome, right click on the element you want and inspect. It will open developer tools and you will have the html line selected already, right click it again-> copy -> copy xpath
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver')
driver.get("http://www.web.com")
driver.find_element_by_xpath(the_xpath_you_copied_before_from_dev_tools).text

id of xpath is getting changed every time in selenium python 2.7 chrome

one more problem i hv,i asked similar question earlier and i tried that method but not able use that methon in this problem so pls help me. it's element
html code is - Filters 
So basically, question is that there is one button its kind of toggle button and i want click on that button to select device like Desktop, Tablet & Mobile all check boxes are already (default) selected now i have to uncheck or deselect device, to do this, first i have to click on that toggle button , when i click on toggle button its id (gwt-uid-598) 598 is getting changed every time or every refresh. Can you pls help me, what should or which method should i follow in this case.
i am using below python code.
Click on device Filters
elem = driver.find_element_by_xpath('//*[#id="gwt-uid-598"]/div/div/span')
elem.click()
Thanks in advance.
Good question.
Try to use another selector, for example: css class or use xpath method contains().
Example: //div[contains(text(), "checkbox")]
I can help you if you can provide source code of the page or needed element.

Select from dropdown using Selenium Python, front end app - react.js

I want to select an item from a drop down using selenium python. The project uses react.js. The dropdown html appears in a div.
code inspect for dropdown
As this is under div, not select, when i try to select specific value, i got an error message.
Error message:
selenium.common.exceptions.UnexpectedTagNameException: Message: Select
only works on elements, not on div
How can I solve this issue?
You cannot use Select class to operate dropdowns which are not implemented using select and option elements.
You have to handle this kind of dropdown "manually" - generally speaking - click it to open it up, locate the desired dropdown item/option and click it. E.g., judging by you concise HTML snippet, to open up the dropdown you can try:
# open up the dropdown
dropdown = driver.find_element_by_css_selector(".Select-control")
# or dropdown = driver.find_element_by_css_selector(".Select-control .Select-input")
dropdown.click()
# TODO: select option
Sometimes, simply focusing the dropdown and typing the desired item/option text would auto-select it - if this is the case, you can try:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element(dropdown).send_keys("Desired option text").perform()
And, if there are any animations or time delays (to, for example, retrieve the options from the server) you may need to add Explicit Waits to handle the possible timing issues.
These are all general tips, I am operating under assumptions and I have no way to check if anything above works for your use case.

Dependent DROP-DOWN selection using selenium and python

I have a form which has two drop-down menu say A and B. Contents of drop-down B is dependent on the selection of drop-down A.
The form uses AJAX to load the contents of drop down B.
I am using selenium and python to automatically select the drop down. I am able to select the drop down A but due to the use of AJAX my code is not working for selecting the content of drop-down B.
I have searched the selenium documentation (Explicit wait) and some stackoverflow answers but still I am unable to implement it in python. I am a newbie in python and selenium so please bear me.
Here is a small portion of my code :
#District selection DROP-DOWN A
district=Select(driver.find_element_by_id("ddlDistrict85"))
district.select_by_value("1")
#SRO selection DROP-DOWN B
# I Need EXPLICIT WAIT logic here to wait till the entire drop-down B is loaded
sro=Select(driver.find_element_by_id("ddlSRO85"))
sro.select_by_value("1")
Suggest some logic to wait till entire drop-down B is loaded.
You can use the options attribute from Select to check you have elements in the dropdown
district=Select(driver.find_element_by_id("ddlDistrict85"))
district.select_by_value("1")
sro=Select(driver.find_element_by_id("ddlSRO85"))
while len(sro.options) == 0:
continue
sro.select_by_value("1")
You haven't shared what the html looks like for these different menus, so let me assume that drop-down B is wrapped by a DIV with a specific class, or even better an ID, perhaps:
<div id="menuB"> ... </div>
Now, you could use Expected Conditions to wait for that menu to appear.
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID,'menuB')))
After searching a lot I found a simple solution where I am able to select the contents of drop-down B.
So I am answering my own question.
Use sleep function from time module to pause the execution of the program for some time(in seconds).
The program code would go like this :
import time #To import time module
#District selection
district=Select(driver.find_element_by_id("ddlDistrict85"))
district.select_by_value("1")
#SRO selection
time.sleep(5)
sro=Select(driver.find_element_by_id("ddlSRO85"))
sro.select_by_value("1")
It's working now.

Categories

Resources