Selenium Finding hover button element by class name in python - python

Happy Sunday,
Trying to click a filter button (called offer) on a website. It is a hover button that does not have an associated link so cannot find element by link text.
When clicking Offer button, the wanted button goes from class="item-list-header-filter-icon item-list-wanted-filter hover-state" to class="item-list-header-filter-icon item-list-wanted-filter hover-state inactive"
I have tried:
driver.find_element_by_class_name(item-list-header-filter-icon item-list-wanted-filter hover-state)
and since it is a dynamic, I made webdriver wait:
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "item-list-header-filter-icon item-list-wanted-filter hover-state inactive"))
)
element.click()
Any ideas much appreciated!
Have a good day

You need to perform a hover over that element.
It can be done as following:
from selenium.webdriver.common.action_chains import ActionChains
offer = driver.find_element_by_css_selector('.item-list-header-filter-icon.item-list-offer-filter')
hover = ActionChains(driver).move_to_element(offer)
hover.perform()
In case all you asked for is to locate that element you can simply do it with
offer = driver.find_element_by_css_selector('.item-list-header-filter-icon.item-list-offer-filter')

Related

How to click radio button with selenium in python?

I want to select a radio button with selenium in python. I have tested with 3 solutions. Unfortunately it doesn't work at all. Could you do me a favor and help me. I am totally beginner.
The URL:
https://biruni.tuik.gov.tr/disticaretapp/menu_ing.zul
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('C:\Webdriver\chromedriver.exe')
driver.get('https://biruni.tuik.gov.tr/disticaretapp/menu_ing.zul')
time.sleep(2)
driver.maximize_window()
element = driver.find_element(by=By.XPATH,value='//*[contains(text(), "Product/Product Groups-Partner Country")]')
element.click()
time.sleep(4)
# radio = driver.find_element_by_id("o1BQ41-real")
# radio.click()
# l=driver.find_element_by_xpath('//*[#id="o1BQ41-real"]')
# l.click()
# driver.find_element_by_css_selector("input#o1BQ41-real").click()
time.sleep(10)
You can use the same select by text to click on the radio button.
radio_element = driver.find_element(by=By.XPATH,value='//*[contains(text(), "Product/Partner Country")]')
radio_element.click()
This selects the desired element
or you can also select the element by id
radio_element = driver.find_element(by=By.XPATH,value='//span[#id="bKFP41"]')
radio_element.click()
That should solve your problem:
element = driver.find_element(by=By.XPATH,value='//html/body/div/div/div/table/tbody/tr/td/table/tbody/tr[3]/td/div/div/table/tbody[1]/tr[13]/td/div/span/span[2]/input')
element.click()
You can't use the element id in XPATH because every refresh the site changes the element id!

python selenium: element click intercepted:

I'm trying to click via find_element_by_xpath() as i did it always. In the case below, there is an error when try to click this element. I'm quite new with selenium and researched already. It seems that i have to click on specific coordination. Do you have any idea how to solve this problem? i'm struggling for quite a while.
section of HTML code:
<map ng-if="!enableSvgHighlighting" id="clickareasVillage" name="clickareasVillage"
ng-show="areas.village.length > 0" class="">
<area ng-repeat="a in areas.village" location-id="32" on-pointer-over="highlightStart(32)" on-
pointer-out="highlightStop(32)" clickable="openBuildingDialog(32)" tg-
coords="758,341,758,342,761,343,763,344,767,345,769"
coords="758,341,758,342,761,343,763,344,767,345,769"
shape="poly" building-positioner="32" class="clickable">
my Code:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//map[#id='clickareasVillage']/area[2]")))
ele1=driver.find_element_by_xpath("//map[#id='clickareasVillage']/area[2]")
ele1.click()
error message:
ElementClickInterceptedException: element click intercepted: Element
<area ng-repeat="a in areas.village" location-id="32"
on-pointer-over="highlightStart(32)" on-pointer-out="highlightStop(32)" clickable="openBuildingDialog(32)"
tg-coords="758,341,758,342,761,343,763,344,767,345,769" coords="758,341,758,342,761,343,763,344,767,345,769"
shape="poly" building-positioner="32"
class="clickable"> is not clickable at point (391, 477).
Other element would receive the click: <img ng-if="!enableSvgHighlighting"
ng-show="areas.village.length > 0" class="clickareas" src="layout/images/x.gif" usemap="#clickareasVillage" data-cmp-info="9">
on other treads at stackoverflow, people suggest to use the following line. If i try, there is a TimeoutExeption.
WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//map[#id='clickareasVillage']/area[2]")))
As said above, usually when you encounter this error it's because a pop-up or an invisible overlay is preventing the driver from clicking on your element.
You said that JS clicking wasn't working either so i'm going to give you 2 quick workarounds that you can try:
ActionChains to move and click on your element:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(element).click().perform()
Send keys using ActionChains
from selenium.webdriver.common.action_chains import ActionChains
for i in range(#determine how much time):
ActionChains(driver).send_keys(Keys.TAB).perform() #tab until element is selected
ActionChains(driver).send_keys(Keys.ENTER).perform() #press enter to "click" on it
Let me know if that helped you.
Try using javascript
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//map[#id='clickareasVillage']/area[2]")))
driver.execute_script("arguments[0].click();", element)
There may be something which could be preventing click on the element, check if any pop-up occuring.

Selenium + Python Click on Search Selects but Does Not Click

I have all of my options selected on this form and have the following code to click the Search button:
WebDriverWait(wd, 10).until(EC.element_to_be_clickable((By.ID, "alMatchFrequencies"))).click()
I have also tried this:
search = WebDriverWait(wd, 10).until(EC.element_to_be_clickable((By.ID, "alMatchFrequencies")))
search.send_keys(Keys.ENTER)
I have also tried subbing in RETURN instead of ENTER.
Here is a screenshot after that line is run:
The search bar is highlighted, but no results are being displayed. I can replicate this screen on my own and am able to press the search button so I don't think it is missing fields.
I believe you are actually clicking the search button but are not seeing the results because they are below the view you show. The scrollable area in your screenshot was a clue.
Clicking the search button is as easy as search.click() after you've executed the selector. But then you need to wait and scroll the page.
To Scroll a page you need to introduceActionChains in the Selenium library and to wait for the load you'll need to use Python's time.sleep(sec) function.
Full code to produce the attached picture:
#Imports
import time
from selenium.webdriver.common.action_chains import ActionChains
#...Code to Load and Select Filters...#
search = WebDriverWait(wd, 3000).until(EC.element_to_be_clickable((By.ID, "alMatchFrequencies")))
search.click()
# Wait for element to load
time.sleep(3)
# Locate element to scroll to
results = WebDriverWait(wd,3000).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/section/div/div[2]/div/div[2]/div[5]/div/div[1]/div[5]")))
actions = ActionChains(wd)
# Scroll
actions.move_to_element(results).perform()
wd.get_screenshot_as_file("test3.png")

Python + Selenium JS dropdown select

I'm totally new, I've gone through some of the examples as you can find these here but as straightforward as it looks I can't make it work.
The page I want to pass: www.webauto.de
My code to select a make, a model and click 'search'.
browser = webdriver.Firefox()
browser.get('http://www.webauto.de')
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'quick-search')))
select = Select(browser.find_element_by_id('carsearchmake'))
select.select_by_visible_text('Ford')
sleep(1)
select = Select(browser.find_element_by_id('carsearchmod'))
select.select_by_visible_text('Fiesta')
browser.find_element_by_xpath('//input[#type="submit"]').click()
sleep(1)
I used the an attribute = value CSS selector to target the submit button.
from selenium import webdriver
d = webdriver.FireFox()
d.get("https://www.webauto.de/")
d.find_element_by_xpath("//select[#id='carsearchmake']/option[text()='Ford']").click()
d.find_element_by_xpath("//select[#id='carsearchmod']/option[text()='Fiesta']").click()
d.find_element_by_css_selector("[value=Suchen]").click()
#d.quit()
Seems you were pretty close.
The xpath:
browser.find_element_by_xpath('//input[#type="submit"]')
doesn't identifies the desired search button uniquely and you can use the following solution:
browser.find_element_by_xpath("//a[#href='https://www.webauto.de/site/de/suchen/' and contains(.,'Erweiterte Suche')]//following::a[1]/input").click()

How can I click on this check box using Selenium Python?

I can click all of the other checkboxes on the page. But when it comes to this one, it won't allow me to click on it
The HTML code for the checkbox is:
<input id="ContentPlaceHolder1_wucSignInStep2_chkTC" type="checkbox" name="ctl00$ContentPlaceHolder1$wucSignInStep2$chkTC">
My code for clicking the text box:
element = driver.find_element_by_xpath('//span[span/input[#name="checkbox checkbox-primary"]]').click()
I can provide the full code if required.
There is an id associated with your input field! You can use the id to find the element
element = driver.find_element_by_id('ContentPlaceHolder1_wucSignInStep2_chkTC').click()
That should do it.
If you are getting an element not visible error then you can try the following:
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_id("ContentPlaceHolder1_wucSignInStep2_chkTC")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
driver.execute_script("arguments[0].click();", element)
The above code will make the element visible and also, put the mouse cursor over the checkbox.

Categories

Resources