So I am trying to write a code to automatically join a google meet. So I need to sign into my google account, which runs all fine but when I get to "Verify that it's you" (image 1), and I try to use driver.get it takes me back to the original sign-in page (image 2)
How can I click on the "Continue" button without using driver.get/without getting taken back to the original sign-in page. I know that all I have to do is click on the "Continue" because when I do it manually it works perfectly. Pressing "Tab" and then "Enter" would also work, but it seems you need to use driver.get as well. Thank you
so i think you don't understand driver.get() usage. it is used only to make the browser browse to a specific link which you can specify before hand. what you have to do here is open the google meet page manually and go to your required page where you want to click the button. right click on the button and click on inspect. then go to the higlighted code and locate the button you want and right click on copy and select xpath from the list. now coming to your code.
add this line:
element=driver.find_element_by_xpath('_xpath')
element.click()
replace _xpath with the long string you copied (which is the xpath of the button you want to click).
this is the way to click on buttons or text boxes or anything you name it in selenium.
**beware of one thing. don't make your code click immediately. if the page does not fully load and the click is made then the 1st line will throw an element not found error. put some kind of delay till page loads or use a while loop and exception handling to wait till the element is found
This one should work for you
userName = driver.find_element_by_xpath("//button[#name='username']")
driver.execute_script("arguments[0].click();", userName)
Related
I use selenium with python and want to mark a checkbox by clicking on it.
After having identified the <input> tag of the checkbox with selenium, I attempt to click on it with
checkbox.click()
However, it throws the ElementClickInterceptedException.
I identify the checkbox I want to click on through a loop. When I try to click on the checkbox outside of the loop (by manually running the code after it was identified and saved to a variable), I found two things:
All things equal, I still get the exception
When I click in the browser window once (that was opened with selenium) and then run checkbox.click(), it works as expected
Any ideas why and how I could attempt to slove this issue (i.e. being able to click on the checkbox within the loop)?
Thanks!
You either deal with the overlapping element or use javascript
driver.execute_script("arguments[0].click();", checkbox)
Sometimes, the checkbox is not directly interactable. You may have to click on the label/text next to the checkbox.
or May be you have to wait till the element is clickable since your are running in a loop.
selenium/chromedriver
When clicking a button, a new tab is opened when I do it through a GUI browser.
Python Selenium seems to have no problem clicking the button, as it gives me no errors. The errors come in the next step, when I need to find an element in the clicked page. I had selenium take a screenshot and it still shows the first page.
Presumably it clicked the button, created a new tab, and didn't switch over?
How do I switch to the new tab, or even verify the new tab exists in the first place?
Thank you!
When you open a browser with selenium, it stores a handle for each tab/window it is controlling in a list called window_handles, for example:
from selenium import webdriver
driver = webdriver.Firefox()
print(driver.window_handles)
...should give you something like ['6b7af9bb-f299-462e-a79a-2b8fda63f388']
When you open a new tab, a new handle for that window/tab should be added to that list. To then switch to the tab you want, use driver.switch_to.window(), for example (continuing above example):
driver.switch_to.window(driver.window_handles[1])
Note: you could also use driver.switch_to_window, but this is deprecated in favor of the above example.
Also, just a tip for debugging, it can be helpful to use the python repl so you can follow what the browser is doing in real time.
I have written a program to go on the Instagram explore page, and like the first six photos.
Right now, I am using this rather convoluted method to find the "Like" button.
I would much rather have it formatted like I did the "click the login button" section.
So far, I have tried inspecting various elements, but I cannot pinpoint the correct one to make it uniquely select the "Like" button. Also, I could just need to use an attribute that I am unfamiliar with to uniquely select the like button.
I am super new to the python and also the selenium, so any help is appreciated.
#like
self.driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/div[1]/article/div[3]/section[1]/span[1]/button').click()
sleep(3)
#Click login button
self.driver.find_element_by_xpath('//button[#type = "submit"]').click()
sleep(3)
to click on like, that web element is in svg tag, so try the below locator :
//button[#type='button']//*[name()='svg' and #aria-label='Like' and #height='24']
something like :
sleep(3)
self.driver.find_element_by_xpath("//button[#type='button']//*[name()='svg' and #aria-label='Like' and #height='24']").click()
Also, I am not sure what do you mean by
#Click login button
I'm creating an Instagram Unfollow Tool. The code usually works, but occasionally Instagram will show the information of some user (as if I hovered over that user) and cause my code to stop running because it obscures the buttons that I need to click in order to unfollow a user.
It's easier to understand with an example:
How can I edit my code to make the mouseover information go away (is there a way to turn off these mouseover effects)?
My code:
for i in range(num_following):
following = self.browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/ul/div/li[{}]/div/div[1]/div[2]/div[1]/span/a".format(i))
following_username = following.get_attribute("title")
if following_username not in followers:
# Unfollow account
following_user_button = self.browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/ul/div/li[{}]/div/div[2]/button".format(i))
following_user_button.click()
unfollow_user_button = self.browser.find_element_by_xpath("/html/body/div[6]/div/div/div/div[3]/button[1]")
unfollow_user_button.click()
print("You've unfollowed {}.".format(following_username))
The error I get:
selenium.common.exceptions.ElementClickInterceptedException: Message: Element is not clickable at point (781,461) because another element obscures it
It seems like the element unfollow_user_button = self.browser.find_element_by_xpath("/html/body/div[6]/div/div/div/div[3]/button[1]") you want to execute .click() on is blocked by a temporary or permanent overlay.
In such cases you either can wait with ExplicitWait and ExplicitConditions till said blocking element has vanished - though this shouldn't work in this specific case as to my knowledge the popup remains if nothing is done. Another approach is to send the click directly to the element by using the JavascriptExecutor:
#Find the element - by_xpath or alike
unfollow_user_button = driver.find_element_by_xpath("XPATH")
#Sending the click via JavascriptExecutor
driver.execute_script("arguments[0].click();", unfollow_user_button)
Note two things:
driver must obviously be an instance of WebDriver.
I would suggest not using the absolute XPath in general. Going with the relative XPath is less prone to be broken by small changes in the site structure. Click here for a small guide to read through.
As the title says, how can I .click() a button using Selenium, when the button gets "disabled" after using the method clear or send_keys?
Before:
That's the page status when I open it's url... but then right after I run my code to find the textbox and replace it's value, the element gets disabled (maybe by some sort of JS) right after I clear it's content or write something to it using send_keys.
After:
Code:
txt_value = driver.find_element_by_xpath('//input[#id="txtValor4"]')
txt_value.clear() #this disables the button
txt_value.send_keys(str(123,45)) #this also disables the button
My question is:
How can I bypass this website protection and press the Continuar button?
I thought about disabling JS, but the whole website relies on it to produces the requires documents.. wrong alternative.
So I thought about using the button properties to simulate the pressing of the button... just don't know if it's possible, or how I could do this.
Another option was blocking only the JS that disables the button maybe mapping where the command comes from using the inspect element and network tools...
So is there any way to achieve this?
ps.: I can't give the URL because it requires my login data.
Ok, so you can't directly do this through normal means. Selenium WebDriver is made to simulate real use of a browser. It may be possible however to use the execute_script function. (Java Selenium has a built in JavascriptExecutor class, I assume this is python's version.) The execute_script function allows Selenium to perform tasks that a human interacting with a browser can't do.
driver.execute_script("document.getElementById('buttonid').click()")
Or something along those lines should work. Hope that helps you out.
If you don't get any solution with selenium and javascript, you can use Sikuli concept. To click that element, take the image of the 'Continuar' button and save it in resources folder.
String elementImg=Path of the Image;
screen.click(elementImg);
I could bypass this using driver.execute_script("document.getElementById('txtValor4').value = 123.45"), to pass the values into the textbox, so the button didn't got disabled and I could press the Continue button.
Even bypassing this, the result wasn't the expected! The value that I entered was supposed to be corrected by some sort of interest. But bypassing this, the value isn't corrected.
Today the user that asked the program told me that everytime I change the value inside this textbox, I must press the Calculate button.
So, instead of inefficiently bypassing this disable method, I could solve my problem using:
b = driver.find_element_by_xpath('//input[#id="txtValor4"]')
b.clear()
b.send_keys('123.45')
driver.find_element_by_xpath('//input[#id="btnCalcular4"]').click()
driver.find_element_by_xpath('//input[#id="btnContinuar4"]').click()
This way the tax value is corrected by interest and the website generate the .pdf with the exact value that I was expecting.
Many thanks for everyone that put some time and effort trying to help me.