I'm trying to make a bot that logs inside my account.
After inserting psw and username I make my bot clicking on "I'm not a robot" recaptcha checkbox.
After flagging the Recaptcha checkbox it could randomly appear a second Recaptcha, this time one with images. Fine, I could solve it by using the audio to text method. The issue is that this Recaptcha appears randomly.
I used an explicit wait but it gets bypassed and doesn't give me any errors.
Here's my code
#solve first recaptcha (I'm not a robot)
driver.switch_to_frame(element) #element = captcha frame already found
recaptcha = driver.find_element_by_xpath('//*[#id="recaptcha-anchor"]/div[1]')
driver.execute_script("arguments[0].click();",recaptcha)
_delay()
driver.switch_to_default_content()
#if an image captcha frame appears click on the audio button
try:
recaptcha_frame = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH,
'/html/body/div/div/div[3]/div[2]/div[1]'))).find_element_by_tag_name('iframe') #locating the recaptcha image frame
driver.switch_to_frame(recaptcha_frame)
audio_button = driver.find_element_by_class_name('rc-button goog-inline-block rc-button-audio')
driver.execute_script("arguments[0].click();",audio_button)
except:
pass
If i would have used driver.quit() instead of pass it would have closed the session. My aim is to click on the audio_button. Any wonder on why this explicit wait doesn't work?
I think you do not need using explicit wait if you already using implicit. This may be the first problem. For the implicit wait the code should look similar to this:
try:
recaptcha = driver.find_elements_by_xpath("Your recaptcha_frame stable xpath")
recaptcha.click()
except NoSuchElementException:
pass
Also, /html/body/div/div/div[3]/div[2]/div[1] is not reliable locator. Change it to the stable one. In your code if this locator is not found, nothing will happen. Use a better exception type to understand the reason.
If you go with explicit wait, try changing presence_of_element_located to visibility_of_element_located
But still, locator is the main problem in your code.
Also, I have doubts you will be able to pass recaptcha.
Related
I came across this problem while automating task with Selenium.
What was the code execution process?
So first of all there're two functions inside seleniumtest.py :
getGateway() and restartRouter().
Let's focus on restartRouter() function as it contains code related to selenium, and getGateway() function was just function that I used in restartRouter() just like this: driver.get(getGateway()).
First of all, what was happening is following:
Start browser and open provided url;
Find elements username and password
Enter username and password;
Find Login element
Click Login;
Find another button
Click on another button
...
What is the problem?
So, the problem start occuring on step 6, just after clicking Login button (See code execution process, step 6).
After it clicks on login button, it loads new page (it doesn't open it in new tab), and after that it didn't want to work.
What have you tried to do to solve the problem
First thing that came up to mine mind was to use driver.wait(5) just after clicking on login, which didn't work.
I've also tried to check if any other element on that page will result in action .click()
After that, I've tried Waits - SELENIUM WAITS ; which also didn't work. I tried two possible options: driver.implicitly_wait(10) and WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//*[#id=\"mmManagDiag\"]")). It also gave me errors how my USB Device isn't working while using explicit waits.
None of these worked.
What worked for me?
After working around it came up to my mind to try with time.sleep(3) and suddenly it worked.
My final question
So, I am wondering why Selenium couldn't find any element on that page before I used time.sleep()?
And isn't selenium waits and time.sleep() almost the same thing?
Code
driver = webdriver.Chrome('./chromedriver')
driver.get(getGateway())
#Frm_Username
username = driver.find_element(By.NAME,'Frm_Username')
username.clear()
username.send_keys("user")
#Frm_Password
passwrd = driver.find_element(By.NAME,"Frm_Password")
passwrd.clear()
passwrd.send_keys("user")
#passwrd.send_keys(Keys.RETURN)
driver.find_element(By.XPATH,"//*[#id=\"LoginId\"]").click()
time.sleep(3)
driver.find_element(By.XPATH,"//*[#id=\"mmManagDiag\"]").click()
time.sleep(3)
driver.find_element(By.XPATH,"//*[#id=\"mmManagDevice\"]").click()
time.sleep(3)
driver.find_element(By.XPATH,"//*[#id=\"Btn_restart\"]").click()
time.sleep(3)
driver.find_element(By.XPATH,"//*[#id=\"confirmOK\"]").click()
driver.quit()
Waiting for the elements presence might be not enough to click on it.
Try to wait for element to be clickable.
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "your xpath value"))
).click()
I am making an automation for download data from a weather institution.
The issue is that in my effort to make it more independent I am trying to make Selenium to Tab keys to a certain spot, so the Browser focus can "Walk" to the download button. When I call the click() function it doesn't do anything. So I tried to Extract the XPath with the function get_attribute("xpath") but it returns None. How I can extract the XPath?
I am going to paste the issue down here:
Bandera=driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div[2]/div/table/tbody/tr[1]/td[1]/div/input')
Bandera.click()
Bandera.click()
## So Here i just select and dis-select a checkbox just to be near the Download button.
actions = ActionChains(driver)
actions.send_keys(Keys.TAB * 1 )
actions.perform()
#Here i just tabed to the button
Accion=driver.switch_to.active_element
#Maybe, here is when i lost the focus of the button?
Descarga_Actual=Accion.get_attribute("xpath")
Thank you and sorry to borrow your time.
To make a click on hover I would use the following sequence:
your_dropdown_locator.click()
dropdown_option = driver.find_element_by_xpath("dropdown option locator")
actions = ActionChains(driver)
actions.move_to_element(dropdown_option)
actions.click().perform()
But, this is the approach that is usually used for dropdowns.
You use: driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div[2]/div/table/tbody/tr[1]/td[1]/div/input') This is the main problem of your code.
If you pasted html code of this dropdown (or button you need to click), people would help you to find a better locator. XPath/CSS must be unique. In your case the locator is very bad.
Also, I see no sense making Bandera.click() two times.
In your case, as I understand, you just need to click the button. So the locator is your main problem.
You need to find the correct locator, to wait till the button is clickable and then to click it.
Another problem in what you are trying to do:
get_attribute("xpath") looks like incorrect expectation of how get_attribute function works. Check at least here what this function means Python Selenium: Find object attributes using xpath
I am trying to write a code that is able to auto apply on job openings on indeed.com. I have managed to reach the last stage, however, the final click on the application form is giving me a lot of trouble. Please refer the page as below
Once logged in to my profile, I go to the relevant search page, click on the listing I am interested in and then on the final page (shown above) I am trying to click on the continue button using xpath as follows:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
driver.find_element_by_xpath('//*[#id="apply-button-container"]/div[1]/span[1]').click()
driver.find_element_by_xpath('//*[#id="form-action-continue"]')
However, this gives me an error:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="form-action-continue"]"}
Having gone through some suggestions on the net I have even tried the following:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
driver.find_element_by_xpath('//*[#id="apply-button-container"]/div[1]/span[1]').click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[#id="form-action-continue"]')))
But then this gives me a timeout error
TimeoutException: Message:
Will appreciate some help on this.
From it seems, on that form there are multiple iframes, therefore the reason for your errors.
You need to get the first iframe, switch to it, get the second iframe inside the first one, switch to it and only afterwards you'll be able to get the continue button.
Something like this should do the trick:
frame_1 = driver.find_element_by_css_selector('iframe[title="Job application form container"')
driver.switch_to.frame(frame_1)
frame_2 = driver.find_element_by_css_selector('iframe[title="Job application form"]')
driver.switch_to.frame(frame_2)
continue_btn = driver.find_element_by_css_selector('#form-action-continue')
continue_btn.click()
Once I had a similar issue and standard time.sleep() until the form (or continue button) appears helped me. Try it instead of WebDriverWait, maybe it will work.
I'm trying to implement an explicit wait before giving a click on a checkbox:
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, "pay_type_list_item_id_salary"]")))
self.driver.find_element_by_xpath('//input[#id="pay_type_list_item_id_salary"]').click()
My problem is that my explicit wait keeps sending the error:
not clickable at point (663, 478). Other element would receive the click.
I'm trying to use different kind of explicit waits like visibility_of_element_located or invisibility_of_element_located (using an element from the previous step on my script), but no luck with those options.
If I add a time.sleep(1) between the 2 lines my scripts works but I know it's not the most efficient thing to use time.sleep.
The previous step before this one opens a calendar and I'm not sure if it tries to give the click when the calendar is closing and that's the reason to receive this error.
The error you are getting is indicating that another element is covering the element you are trying to click. If you look at the error message (you really should post the full error message in your question), it will tell you the HTML of the element that is in the way. That will give you a good idea of what element is blocking the click so you can find it and figure out what part of the page it is. Then you can wait for it to get out of the way. From your description, it sounds like you need to add a wait for the calendar to close.
I have web page, where the user needs to upload a list of documents and only after which the Submit button is enabled.
I am using python selenium for this automation. My program is able to upload the documents but is unable to click on the Submit button after it is enabled.
I tried this:
element = WebDriverWait(driver, 10000).until(EC.element_to_be_clickable("(//button[#type='submit'][2]"))
element.click()
but it is not working, as the jobs are not submitted in the front end
Change code to be:
element = WebDriverWait(driver, 100).until(
EC.element_to_be_clickable((By.XPATH, "//input[contains(#ng-click,'SubmitJob')]")))
element.click()
So we now are waiting for up to 100 seconds (instead of 3 hours), and we are passing a tuple argument (By.XPATH, "//input[contains(#ng-click,'SubmitJob')]") to EC.element_to_be_clickable, which is what it expects.
Cannot comment on the correctness of the xpath though, but please check it as well.
Edit: changed the xpath based on comment. There are many ways to express that xpath. I would prefer ng-click over class attribute, since classes may change; action, however, will likely stay the same. But if you choose using classes, I still suggest going with something like
//input[contains(#class,'btn') and contains(#class,'form-control')]
because you never know if the order of classes will change.