bypass youtube account pop-up with selenium in python - python

i have tried to get past this pseudo pop up in the following ways:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID , 'introAgreeButton'))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH , '//*[#id="introAgreeButton"]'))).click()
none of the ways described seem to work.
is there any way to bypass this annoying pop-up ?

Ive came across on same problem. The solution is:
try:
driver._switch_to.frame("iframe")
aggreeButton = driver.find_element_by_id("introAgreeButton")
aggreeButton.click()
except:
print("failed")
to move out the current frame to the page level
driver.switch_to.default_content()

This answer is updated for July 2022
if 'consent.youtube.com' in driver.current_url: # THIS IS THE CHECK
driver.find_element_by_xpath('//button[#class="VfPpkd-LgbsSe VfPpkd-LgbsSe-OWXEXe-k8QpJ VfPpkd-LgbsSe-OWXEXe-dgl2Hf nCP5yc AjY5Oe DuMIQc LQeN7 IIdkle"]').click()
This will skip the 'Before you continue to Youtube screen'

I can't get that pop-up to occur on my attempts when I navigate to YouTube; but, I suspect that this is a dialog or alert. If you check the Elements tab using the Chrome Dev Tools ( F12 ), that should tell you. Then, use the find_element by xpath to click on "I agree"
UPDATED - Modified XPATH to the iron-overlay-backdrop tag
driver.find_element(By.XPATH, "//iron-overlay-backdrop[#class='opened']//button[text()='I agree']").click()
After that, wait for the modal / dialog to disappear or be removed from the DOM and continue doing what you're doing

Related

Selenium cannot find class [duplicate]

I'm trying to automate a process on this page, and according to its html code, after clicking the wallet button located at the top right corner of that page, it deploys 4 main wallets to choose to log in to the page.
All of those wallets share the same class which is elements__StyledListItem-sc-197zmwo-0 QbTKh, and I wrote the code below in order to try to get their button names (Metamask, Coinbase wallet...), here:
driver = webdriver.Chrome(service=s, options=opt) #execute the chromedriver.exe with the previous conditions
driver.implicitly_wait(10)
driver.get('https://opensea.io/') #go to the opensea main page.
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="__next"]/div/div[1]/nav/ul/div[2]/li/button'))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, '//*[#id="__next"]/div/div[1]/nav/ul/div[2]/li/button')
wallet_button.click() #click that wallet button
wallet_providers = driver.find_elements(By.CLASS_NAME, "elements__StyledListItem-sc-197zmwo-0 QbTKh") #get the list of wallet providers
for i in wallet_providers:
print(i)
After compiling the code above, I noticed that it didn't print anything, and it was due to the empty array of wallet_providers, which is very weird because I understand that by calling find_elements(By.CLASS_NAME, "the_class_name") it will then return an array containing the elements that share the same class, but it didn't do that in this case.
So, I would appreciate if someone could explain me what did I do wrong? In the end, I just wanted to manage to click on the Metamask button which doesn't always stay at the same location, sometimes it's the first element of that list, sometimes the second...
You are using this CLASS_NAME elements__StyledListItem-sc-197zmwo-0 QbTKh which has space in it.
In Selenium, a class name having space will not be parsed and will throw the error.
The reason why you did not get the error is cause you are using find_elements that will either return a list of web element or nothing.
So how to resolve this?
remove the space and put a . instead to make a CSS_SELECTOR
try this:
wallet_providers = driver.find_elements(By.CSS_SELECTOR, ".elements__StyledListItem-sc-197zmwo-0.QbTKh") #get the list of wallet providers
to be honest we can have better locator than this, cause it seems these values 197zmwo-0.QbTKh are generated dynamically.
I would rather use this CSS:
li[class^='elements__StyledListItem'] span[font-weight]
or this xpath:
//li[starts-with(#class,'elements__StyledListItem')]//descendant::span[#font-weight]
Also, you should print it like this: (this is one way but there are others as well):
Code:
driver.get("https://opensea.io/")
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="__next"]/div/div[1]/nav/ul/div[2]/li/button'))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, '//*[#id="__next"]/div/div[1]/nav/ul/div[2]/li/button')
wallet_button.click() #click that wallet button
wallet_providers = driver.find_elements(By.CSS_SELECTOR, "li[class^='elements__StyledListItem'] span[font-weight]") #get the list of wallet providers
for i in wallet_providers:
print(i.get_attribute('innerText'))
Console output:
WalletConnect
MetaMask
Coinbase Wallet
Fortmatic
Process finished with exit code 0
The locators you are using are not relative enough, and on my first inspection, I somehow didn't locate them in the DOM. So, refactored code with relative locators to make the code work.
driver.get('https://opensea.io/') #go to the opensea main page.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[#title='Wallet']"))).click()
wallets = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[#data-testid='WalletSidebar--body']//li")))
for wallet in wallets:
print(wallet.text)
Output:
WalletConnect
MetaMask
Popular
Coinbase Wallet
Fortmatic
Process finished with exit code 0
You use the class name elements__StyledListItem-sc-197zmwo-0 QbTKh which has space in it to find the elements. Actually, in Selenium we can't use the class name to locate an element/elements which have space in it. You can use CSS-Selector instead of the class name and in CSS-Selector you need to replace the spaces of the class with a (.) dot.
OR
You can use the parent class and then tags to point to the desired elements.
div[class='Blockreact__Block-sc-1xf18x6-0.eOSaGo'] > ul > li

Selenium Webdriver failing to click. Unsure why

I have the following code;
if united_states_hidden is not None:
print("Country removed successfully")
time.sleep(10)
print("type(united_states_hidden) = ")
print(type(united_states_hidden))
print("united_states_hidden.text = " + united_states_hidden.text)
print("united_states_hidden.id = " + united_states_hidden.id)
print(united_states_hidden.is_displayed())
print(united_states_hidden.is_enabled())
united_states_hidden.click()
The outputs to the console are as follows:
Country removed successfully
type(united_states_hidden) =
<class 'selenium.webdriver.remote.webelement.WebElement'>
united_states_hidden.text = United States
united_states_hidden.id = ccea7858-6a0b-4aa8-afd5-72f75636fa44
True
True
As far as I am aware this should work as it is a clickable web element, however, no click is delivered to the element. Any help would be appreciated as I can't seem to find anything anywhere else. The element I am attempting to click is within a selector box.
Seems like a valid WebElement given you can print all of the info. like you did in your example.
It's possible the element located is not the element that is meant to be clicked, so perhaps the click is succeeding, but not really clicking anything.
You could try using a Javascript click and see if that helps:
driver.execute_script("arguments[0].click();", united_states_hidden)
If this does not work for you, we may need to see the HTML on the page and the locator strategy you are using to find united_states_hidden so that we can proceed.

Selenium not following click that opens a new tab

For the longest time I had an issue with selenium finding a button I was looking for on the page. After a week I got the "brilliant" idea to check the url that selenium was searching the button for. Dummy me, it was the wrong URL.
So the issue is, selenium searches page1 for a specific item. It then clicks it, and by the websites design, it opens page2 in a new tab. How can I get selenium to follow the click and work on the new tab?
I though of using beautiful soup to just copy the url from page1, but the website doesn't show the urls. Instead it shows functions that get the urls. It's really weird and confusing.
Ideas?
all_matches = driver.find_elements_by_xpath("//*[text()[contains(., 'Pink')]]")
item = all_matches[0]
actions.move_to_element(item).perform()
item.click()
try:
print (driver.current_url)
get_button = driver.find_elements_by_xpath('//*[#id="getItem"]')
except:
print 'Cant find button'
else:
actions.move_to_element(get_button).perform()
get_button.click()
Selenium treats tabs like windows, so generally, switching to new window/tab is as easy as:
driver.switch_to.window(driver.window_handles[-1])
You may find it helpful to keep track of the windows with vars:
main_window = driver.current_window_handle
page2_window = driver.window_handles[-1]
driver.switch_to.window(page2_window)
Note that when you want to close the new window/tab, you have to both close & switch back:
driver.close()
driver.switch_to.window(main_window)

Web-element is visible and enabled but .click() fails in python selenium with phantomJS

I want to click on the Next-button at https://free-proxy-list.net/. The XPATH selector is //*[#id="proxylisttable_next"]/a
I do this with the following piece of code:
element = WebDriverWait(driver, 2, poll_frequency = 0.1).until
(EC.visibility_of_element_located((By.XPATH, '//*[#id="proxylisttable_next"]/a')))
if (element.is_enabled() == True) and (element.is_displayed() == True):
element.click()
print "next button located and clicked" # printed in case of success
Subsequently, I get all the IPs from the table like this:
IPs = WebDriverWait(driver, 2, poll_frequency = 0.1).until
(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ':nth-child(n) > td:nth-child(1)')))
Although the CSS_selector is the same for all tabs, and although I get a next button located and clicked, the IPs output is the same for both tabs (i.e. it seems like the Next-button never was clicked). Additionally, there is no Exception thrown.
Therefore, there must be something fundamentally wrong with my approach.
How to click on visible & enabled buttons correctly in phantomJS using python/selenium?
For your understanding, here is the html of the page section I am referring to:
As far as I see there could be two possible causes:
The click was not registered, though this is highly unlikely. You can look at other ways to click like JavascriptExecutor's click.
(Most likely) The find elements are queried right after the click is performed and before the Page 2 results are loaded. Since elements is visible from page 1, it exits immediately with the list of elements from page 1. An ideal way of doing this would be (using psuedocode as I am not familiar with python)
a. Get the current page number
b. Get all the IPs from the current page
c. Click Next
d. Check if (Current page + 1 ) page has become active (class 'active' is added to the Number 2)
e. Get all the elements from the current page
I am the OP and for anyone coming across a similar problem - The Next element was no longer attached to the DOM following its selection, which caused StaleElementReferenceException when printing element.is_enabled() or when clicking it - a detailed solution can be found here

Using python selenium to click an element not visible

I am using python selenium on browser to fill in some form. I was trying to select an element in the drop-down list,
0
but if I try to find it by text using this script:
browser.find_element_by_link_text("0").click()
it result an error:
"unknown error: Element is not clickable at point (498, 612). Other element would receive the click: ..."
and if try to find it by class name:
browser.find_element_by_class_name("dropdown-toggle").click()
it result in another error: "element not visible"
is there any way I can click onto that drop-down list? Thanks very much.
I had a similar problem. You can execute a script to change the visibility of that element once you find it and then click it.
driver.execute_script("arguments[0].style.visibility = 'visible';",myElement)
myElement.click()
Try to find it by Xpath searching for partial class and text at the same time:
browser.find_element_by_xpath(//a[contains(#class, 'dropdown-toggle select') and contains(text(), '0')]).click();
you should get the element by xpath and then press it.
browser.find_element_by_xpath(xpath).
read here how to get the xpath: http://www.wikihow.com/Find-XPath-Using-Firebug
Thanks for the feedback, I've found the problem which was due to the new script being loaded by javascript as triggered by some clicks. I've created the following code to capture the exception and retrying until the element is ready:
while True:
try:
time.sleep(1)
browser.find_element_by_xpath("//div[#class='tckt']/a").click()
print("found")
break
except ElementNotVisibleException:
print("notvisible")
except WebDriverException:
print("clickduplicate")
I read it on articles says this happened a lot on radio button for Chrome webdriver, hope this helps.
Firstly click the parent element by finding it, using it's xpath->find the element you want to click within the parent element like the way you did. i.e
browser.find_element_by_link_text("0").click()
Hope this 2 steps will work for you. Otherwise please post the url with the code you've tried-It'll be easy to find out the issue.

Categories