Select an li element in a listbox with Selenium - python

I have a python script where i'm using Selenium to do some testing.
Currently I am trying to select some elements in multiple list boxes.
You can check out the site here: http://www.guloggratis.dk/annonce/opret (The language is Danish, if you are wondering)
So what I want to do is select a category, then some new categories pops up and I select one of those until there are no more categories left.
I can select the first element in the first box like this:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.guloggratis.dk/annonce/opret')
browser.find_element_by_css_selector("span.pos-absolute").click()
browser.find_element_by_id("categoryId_597").click()
While this works, I don’t know why i need to do
browser.find_element_by_css_selector("span.pos-absolute").click()
Anyways if I try select a category from the list that appears in the second box like this
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.guloggratis.dk/annonce/opret')
browser.find_element_by_css_selector("span.pos-absolute").click()
browser.find_element_by_id("categoryId_597").click()
browser.find_element_by_id("categoryId_598”).click()
it doesn’t work even though "categoryId_598” is an id of one of the new categories.
I appreciate any help you might provide and I look forward to hear your answer.
If you have a problem reproducing the error, make a comment and I’ll instantly respond from my phone.

Maybe because of the cookie window?
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.guloggratis.dk/annonce/opret')
browser.find_element_by_id("cookieClose").click()
browser.find_element_by_id("categoryId_597").click()
browser.find_element_by_id("categoryId_598").click()
works fine for me.

Related

Accepting cookies popups using selenium in python

I'm trying to put together a small scraper for public trademark data. I have a database available that i'm using selenium and python to access.
I can do just about anything I need to be able to, but for some reason i can't actually click the "accept cookies" button on the website. The following code i use highlights the button, but it does not get rid of the popup.
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
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
driver.get('https://data.inpi.fr/recherche_avancee/marques')
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "tarteaucitronPersonalize2"))
).click()
I have looked up similar threads on this forum, and I have tried multiple things :
- adding a waiting period, that ended up highlighting the button, so at least i know it does something
- using javascript code to do the actual click, did not work any better
- i tried calling the button via its ID, its XPATH, its CSS selector, anything i could find really
I even downloaded Selenium IDE to record my clicks to see exactly how I could replicate it, but it still only recorded a click.
I tried my best, does anyone know where my mistake lie ? I am open to using other languages, or another platform
Well it looks like I managed to solve it just minutes after posting my question !
For some reason you need to resize the window. I just added the following line of code after opening the URL and it worked first time.
driver.maximize_window()
I added this answer in case anyone stumbles upon this post and wants to avoid pulling their hair out over this !

Python selecting with (java-enabled?) dropdown selection

am new to coding and have been trying to select a dropdown box to select underlying options. Have been able to interact with other drop-down on this page, though this particular one seems different as I've been unable to access/interact with it
I've included a snapshot of the webpage's drop-down box, source codes for the specific drop-down box, and codes that I've already tried and had errors with.
Would appreciate any help/pointers. Am a complete newb to coding (python or HTML!), but know a little about Excel VBA.
website: https://www.firstsentierinvestors.com/sg/en/retail/performance/price-and-performance.html
Tried to paste a picture though it seems I'm too new to do so directly. I'm trying to interact with the "Select Fund" drop-down box on the page. Have tried the following codes:
browser.find_element_by_id('strategy').send_keys(fund_strat) #works fine
browser.find_element_by_xpath('//*[#id="share-class"]').click()
#this line directly above does not work, including variants below:
browser.find_element_by_xpath('THE BELOW XPATHS').click()
//*[#id="price-performance-form"]/div[3]
//*[#id="price-performance-form"]/div[3]/div
//*[#id="price-performance-form"]/div[3]/div/a
//*[#id="price-performance-form"]/div[3]/div/a/span
//*[#id="share-class"]
//*[#id="share-class"]/option[1]
Also tried using CSS selector
browser.find_element_by_css_selector('a.option-selected').click()
browser.find_element_by_css_selector('a.option-selected').send_keys('NAME OF FUNDS ETC')
Image describing problem
If the below line works fine for you then,
browser.find_element_by_xpath('//*[#id="share-class"]').click()
#this line directly above does not work, including variants below:
You need to wait for the element which is visible on the DOM.
i.e.,css = <div class="custom-select custom-select-open">
These days most of the web apps are using AJAX techniques.
So you need to add a wait duration (till the element gets visible on the page or get loaded on the DOM).
So, Try using Selenium Waits(put below code):
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XX, 'element')))
XX = CSS, XPATH, ID, etc.
Now, If you want to write on the selected text box then just try to send keys on input element.
browser.find_element_by_css_selector('.editable-select input')
for much better selector use parent elements to locate the exact element.
CSS = 'div[class$="custom-select custom-select-open"] .editable-select input'
If you want to select dropdown options and you know the Values. Try using below CSS.
browser.find_element_by_css_selector('select option[value$="Your Value"]')
or
If you don't know the values then please attach whole DOM feed after typing into the text box. I'll try to help you out with the Dropdown selection of item.
Else, there is few more steps are there that you need to workout. Get all elements using inside text.
Note: You can approach the text search of an element using XPATH. Example : xpath = "//*[contains(text(),'your text')]"

Element not intractable Selenium

I'm trying to make an auto bump program for forums but I can't seem to interact with anything.
import webbrowser
import pyautogui
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://ogusers.com/member.php?action=login")
#Login
inputElement = driver.find_element_by_class_name("textbox")
inputElement.send_keys("xKyle")
Can't really tell where problem lies, you need to say exactly what is happening, let's start with not giving selenium path to ChromeDriver. You need to download it from here, make sure to get driver compatible with your browser verison.
Next give path to it, if you are on windows path will look like:
r"C:\dir\dir\chromedriver.exe'
you need to give 'r' for raw data, otherwise interpreter will think that you are trying to give some weird in-string command like \n for new line
Also try not to search by class name, for 99% of time you want to use Xpath, especially if it's relative. So just click on element, and copy xpath.
For more answers you need to tell exactly what are you trying to do and when it fails :)

Why does trying to click with selenium brings up "ElementNotInteractableException"?

I'm trying to click on the webpage "https://2018.navalny.com/hq/arkhangelsk/" from the website's main page. However, I get this error
selenium.common.exceptions.ElementNotInteractableException: Message:
There's nothing after "Message:"
My code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.get('https://2018.navalny.com/')
time.sleep(5)
linkElem = browser.find_element_by_xpath("//a[contains(#href,'arkhangelsk')]")
type(linkElem)
linkElem.click()
I think xpath is necessary for me because, ultimately, my goal is to click not on a single link but on 80 links on this webpage. I've already managed to print all the relevant links using this :
driver.find_elements_by_xpath("//a[contains(#href,'hq')]")
However, for starters, I'm trying to make it click at least a single link.
Thanks for your help,
The best way to figure out issues like this, is to look at the page source using developer tools of your preferred browser. For instance, when I go to this page and look at HTML tab of the Firebug, and look for //a[contains(#href,'arkhangelsk')] I see this:
So the link is located within div, which is currently not visible (in fact entire sub-section starting from div with id="hqList" is hidden). Selenium will not allow you to click on invisible elements, although it will allow you to inspect them. Hence getting element works, clicking on it - does not.
What you do with it depends on what your expectations are. In this particular case it looks like you need to click on <label class="branches-map__toggle-label" for="branchesToggle">Список</label> to get that link visible. So add this:
browser.find_element_by_link_text("Список").click();
after that you can click on any links in the list.

Web Scraping using Selenium with two drop down menus

I am trying to make a web scraper that cycles through two drop down menus, but I cannot seem to locate the first drop down box using selenium. I was to cycle through all the names, and years in the drop down box and export a table of all pages and values to a csv. The webpage is :http://surge.srcc.lsu.edu/s1.html
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://surge.srcc.lsu.edu/s1.html")
element = driver.find_element_by_xpath('//select[#id="storm_name"]')
all_options = element.find_elements_by_tag_name("option")
My error is:
NoSuchElementException: Unable to locate element:
{"method":"xpath","selector":"//select[#id=\"storm_name\"]"}
So because of #HumphreyTriscuit I will post this as an answer. Please take the time to mark it as an answer in case it was.
So there you go:
Its because you are not loading the right webpage to find your element. Your webpage contains an iFrame that points to the actual content but Selenium I believe is not able to pre-load iFrames. Get this page and your code should work.
And also a little bonus right here: If you are unsure about your xPath you should consider using xPath Checker for Firefox.

Categories