Finding elements after click using Selenium - python

I can't figure this one out.
I am working on this site and I have used selenium in Python to click on the first element under the class name "yellow showPopupUnder" (the main part of the screen where there are 20 yellow rows of information about houses).
After I get Selenium to click on that row it opens up and shows more information. I am interested in the part where there are 'checked' and 'unchecked' boxes. Those checked boxes are in a div like so:
<div class="v_checked">
And the unchecked boxes are in a div like so:
<div class="v_unchecked">
I have tried reaching them in a few ways:
driver.find_element_by_class_name('v_checked')
driver.find_element_by_css_selector(".v_checked")
driver.find_element_by_xpath("//div[#class='v_checked']")
I have also tried to use the absolute xpath. All of these don't work and I get a "NoSuchElementException: no such element: Unable to locate element"
Does anybody know how to retrieve the data from those boxes?
Thank you!

There is a iframe used that opens up and shows more Information where those Checkbox are shown. You have to navigate to that iframe. You can do something like this:
## switch to the iframe ##
driver.switch_to_frame(driver.find_element_by_id("ad_iframe_2_1_1731213"))
## then you can search your checkbox##
driver.find_element_by_class_name('v_checked')
## Switch back to the "default content" (that is, out of the iframes) ##
driver.switch_to_default_content()

Related

Click checkbox with python selenium

so I wanted to click a checkbox on website using selenium (python).That's the button I want to click
So I thought that it would work with that code:
driver.find_element_by_xpath("//input[#name='termsCheck']").click()
But that gives me an errorThat's the error I get
Additional info: there are 2 more checkboxes on the same page which have also <span class="custom-checkbox"> ::before ::after </span>
Has anyone an idea how to get selenium to click the checkbox?
I have seen some scenarios were the element must be clicked with javascript because it is covered by other elements. Alternatively you could click the <span> element that is covering it.
Here is how to click the element with javascript using python and selenium. Since you have not provided the HTML I am assuming that the xpath you provided uniquely identifies the element you want to click.
element_to_click = driver.find_element_by_xpath("//input[#name='termsCheck']")
driver.execute_script("arguments[0].click();", element_to_click )
On most browsers you should be able to copy the XPath or CSS selector by right clicking the specific element on the developer tools console. The click() method should work.
The code is attempting to click the checkbox and Selenium API doesn't like that. The error informs about that, but is not specific enough. Try using auxiliary class Select instead:
from selenium.webdriver.support.ui import Select
element = driver.find_element_by_xpath("//input[#name='termsCheck']")
select = Select(element)
select.select_by_index(index)
Additionally, make sure that XPath //input[#name='termsCheck'] is only matching single element.
Refer to Selenium Python documentation for more details.

clicking on button no ID in Selenium python

Hello I am trying to click on a button within python selenium that has no ID. The button has the text Submit an application within it. However there are multiple phrases of "Submit an application" on the website. The attributes of the website are:
<a class= "button" href = "(a link)" analytics-on="click" analytics-category="Body" analytics-event="(a link)" analytics-label="Submit an application||(a link)">
According to the documentation, there are multiple methods you can use to locate an element.
In this situation, I would personally use Xpath if available. Open the chrome developer tool on the HTML tab and find the element you want to locate (or right click on the element and go to inspect element). Right click on the HTML tag (in the dev tool) and select copy > xpath.
Now in your python code you can locate your element like so:
button = driver.find_element_by_xpath("past_the_xpath_here")
Alternatively because you are trying to locate a link, you could also locate the element using the link text:
button = driver.find_element_by_link_text("Submit an application")
you can try with css_selector.
driver.find_element_by_css_selector('[analytics-on="click"]').click()
driver.find_element_by_css_selector('[analytics-category="Body"]').click()
or link_text method is also a good choice in locating an a tag,
driver.find_element_by_link_text("Submit an application").click()

How to click a menu item from mobile based website in selenium Python?

So, I was doing some automation for Google Play Store(website from computer).
This is the element I am looking to click.
This is the menu which opens when we click the 3 dots on side of review. I have to click one of these 3 menu options then. I am not able to use .click() function on any of these 3 items.
All of these 3 span elements of menu are contained in a single div parent.
When I use
element = driver.find_element_by_xpath('/html/body/div[1]/div[4]/c-wiz/div/div[2]/div/div/main/div/div[1]/div[3]/div/div[2]/div[1]/div[2]/div/div[2]/div[2]/div/div/div/span[2]/div[2]')
element.click()
It gives:
ElementNotInteractableException: Message: Element <div class="uyYuVb oJeWuf"> could not be scrolled into view
I tried to click the span, and also its child elements like div which has display:flex also the last div child. But all of them gave the same exception. I know Play Store is mainly for mobile and therefore it is showing the properties of a mobile element. What is the appropriate way to click any of these 3 options then?
here is the link:
I don't think it is possible to click on a span like this one using selenium. However I think you should try getting the position of this span on the screen so you can click on it using a module like pyautogui :
import pyautogui
pyautogui.click(x=positionx, y=positiony)
This is the only solution I found and I hope you will not have to do it a lot of time in your program since it's repetitive and annoying (to find the x and y positions)

Can't find the button to click with selenium

Trying to click on the button on "https://euw.op.gg/summoner/userName=JengaSneaky". But I can't find the element that trigger it.
The pic shows one of the buttons. I want Selenium to click on it so I can scrape the data that pops up. I've tried to find the element but it says I can't use it with click().
The problem with these kind of buttons is that they are generated each time you load the html ( or the page ) based on the data contained in a database that changes everyday.
That's why they don't come with a unique id or something to distinguish them from others.
The thing you could do here is to find them by the CSS SELECTOR or by XPATH.
But you will always have to check if they changed everyday.
What you could also try to do is locate them if they contain a specific text.

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.

Categories