Python selenium doesn't click? - python

In python I have:
driver = webdriver.Safari()
driver.get("https://ug3.technion.ac.il/rishum/login")
driver.find_element_by_id("username").send_keys(user_id)
driver.find_element_by_id("password").send_keys(password)
driver.find_element_by_class_name("btn btn-success").click()
But for some reason it doesn't click on sign bottom (the green one) how can I fix this?
Update: from the comments I got I understand that I need to solve the captcha thus I want now to click on the Audio bottom, in a tutorial they wrote:
driver.switch_to.default_content()
frames = driver.find_element_by_xpath("/html/body/div[2]/div[4]").find_elements_by_tag_name("iframe")
driver.switch_to.frame(frames[0])
delay()
# click on audio challenge
driver.find_element_by_id("recaptcha-audio-button").click()
but that didn't work for me.

Related

Selenium can't found valid item from iframe

I have code:
self.driver.execute_script(f'document.getElementsByName("h-captcha-response")[0].setAttribute("h-captcha-response", "{token}")')
self.driver.execute_script(f'document.getElementsByName("g-recaptcha-response")[0].setAttribute("g-recaptcha-response", "{token}")')
self.driver.find_element_by_id('checkbox').click()
time.sleep(5)
and that code don't click at the item with id checkbox (check screenshot)
i think the iframe upper makes big problems (check screenshot)
if this is right version of solving problem, how to refocus Selenium onto that iframe?
I dont think that driver.switchTo().frame(iframe); would work but
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#name,'a-')]")));
might.

Why won't Selenium in Python click the pop-up privacy button?

I am having some issues with Selenium not clicking the pop-up privacy button on https://www.transfermarkt.com/
Here is my code so far:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
It comes up saying:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button"}
Anyone have any thoughts?
I believe the issue is that the element you are trying to click is inside an iframe. In order to click that element you'll have to first switch to that frame. I noticed the iframe has title="SP Consent Message" so I'll use a CSS Selector to identify it based on that. Your code with the added line:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
Note that you may have to switch back to the default frame to continue your test, I'm not 100% sure as that iframe has gone away.
Also it seems like some folks don't get that popup when the hit the website, not sure why, may be something you want to look in to to determine how you want to test this.
Like #Prophet says you should improve the xpath for the button (again it seems to have a unique title so I would use CSS Selector 'button[title="ACCEPT ALL"]'), but this works for me to click it.
There are 2 issues here:
You need to add wait / delay before accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button') to let the page load
It's very, very bad practice to use absolute XPaths. You have to define an unique, short and clear related XPath expression.
Additionally, I see no pop-up privacy button when I open that page. So, maybe that element is indeed not there.

Fails to perform keys down and hit enter in context menu python selenium

I want to login to a site. Right-click on one of the links and open in New Tab or New Window.
I have searched earlier here and googled around before posting it here. May be I am doing it wrong
button=browser.find_element_by_link_text('Menus');
action=ActionChains(browser)
action.context_click(button).perform() #--> Till here working fine, Right clicks on Menu
action.send_keys(Keys.ARROW_DOWN+Keys.ARROW_DOWN+Keys.ENTER).perform() #--> Not Working
I wouldn't go into that direction as performing context menu clicks will bite you back when you'll be running your Selenium tests in Parallel Mode
Instead of opening context menu and clicking I would rather recommend:
Extract href attribute from the link
Use Window.open() JavaScript function to open the link in the new tab
Use Explicit Wait to wail until number of windows becomes 2
Switch the context to the new tab
Example code:
button = browser.find_element_by_link_text('Menus')
href = button.get_attribute("href")
browser.execute_script("window.open('" + href + "')")
WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
browser.switch_to.window(browser.window_handles[1])

Python Selenium Recaphta I am not a robot - how to bypass

First of all, I am not by any means an expert scraping the web, selenium, or have a good understanding of html...
I am trying to download a CSV file that is hidden behind a 'recaptcha - i am not a robot'. I first must login to the website and then there is link that generates the I am not a robot captcha. It is the kind that you just click a box and must hit 'yes, i am not a robot' button.
I can't figure out how to get this to work. I've seen things about finding the x/y coordinates of where the box pops up and clicking, but I can't seem to figure that out. There seems potentially other methods, but I'm just having a hard time deciphering what to do and then how to do. My code thus far...
import selenium
browser = webdriver.Chrome()
browser.get("mywebsite")
username = browser.find_element_by_id("Email")
password = browser.find_element_by_id("Password")
username.send_keys('<<MY_EMAIL_FOR_LOGIN>>')
password.send_keys('<<MY_PASSWORD_FOR_LOGIN>>')
browser.find_element_by_xpath('//*[#id="container"]/div[3]/div[1]/div[1]/form/div[2]/div[2]/div[1]/input[6]').click() #button to 'login'
# so far so good - I am logged in!
elements = browser.find_element_by_id("patient-export-button")
elements
browser.execute_script("arguments[0].click();", elements)
# the lines above will cause the recaptcha popup to appear.
# now I am STUCK!
I have to now click the checkbox & hit 'yes' in order for my file to download. Sometimes when I hit the checkbox I get all these stupid 'find the car' images which are a pain in the ass for me to solve even as a human...
Any help on solving this recaptcha thing would be appreciated.

Selenium + Python - After clicking on a button, how to I catch the pop up window (specifically, an upload image pop up)

I'm trying to upload images to Microsoft's http://how-old.net/ API to compare with our age and gender classification algorithm [CVPR AMFG15].
I'm using Selenium and it's pretty easy to navigate to the web-site and click the "Use your own photo" button:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://how-old.net/")
elem=driver.find_element_by_id("uploadFileId")
elem.click()
I tried various solution that I found online:
upload_window=driver.find_element_by_partial_link_text("File")
Or:
driver.SwitchTo().defaultContent();
Nothing seems to work. Again, finding the button and clicking it is very easy, catching the window and uploading an image seems the hard part.
EDIT:
I've also tried the following:
driver = webdriver.Firefox()
driver.get("http://how-old.net/")
file_input=driver.find_element_by_id("uploadBtn")
driver.execute_script("arguments[0].style.visibility='visible';", file_input)
file_input.send_keys('image_name.jpg')
But I get the following exception:
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
Even though from what I see, the button is visible (see attached print screen).
[CVPR AMFG15] Levi, Gil, and Tal Hassner. "Age and Gender Classification using Convolutional Neural Networks."
You cannot control the file upload window with selenium.
The common approach to solve the problem is to avoid it being opened in the first place.
Find the file input and pass the path to the file using send_keys():
file_input = driver.find_element_by_id("uploadBtn")
file_input.send_keys("/absolute/path/to/file")
This would not work "as is" since the file input is hidden, make it visible first:
driver.execute_script("arguments[0].style = {visibility: 'visible'};", file_input)

Categories

Resources