Handle a click when hyperlinks are not visible - python

I am new to selenium and trying to automate some web click using selenium and python
There is a link as per below, which opens up dialogue box
<li id='upFol'>
<a href='#' title='documents'></a>
.....
</li>
I have below code in python
upload = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH,"//*[#id='upFol']/a")))
upload.click()
this finds the element and can see the click but dialogue box doesn't get opened
Is there way to handle these kind of scenarios

Instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li#upFol > a[title='documents']"))).click()
Using XPATH:
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//li[#id='upFol']/a[#title='documents']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Accepting a Discord Invite using Selenium

I'm trying to create a simple script to accept a Discord Invite. I've managed to login to an account however I cannot get it to accept the invite.
I've tried using the XPath aswell as using CSS selector to try and find the button but i'm having no luck (examples seen below).
browser.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/div/div/section/div/button")
browser.find_element_by_class_name('marginTop40-i-78cZ.button-3k0cO7.button-38aScr.lookFilled-1Gx00P.colorBrand-3pXr91.sizeLarge-1vSeWK.fullWidth-1orjjo.grow-q77ONN')
The outer html is:
<button type="button" class="marginTop40-Q4o1tS button-1cRKG6 button-f2h6uQ lookFilled-yCfaCM colorBrand-I6CyqQ sizeLarge-3mScP9 fullWidth-fJIsjq grow-2sR_-F"><div class="contents-3ca1mk">Accept Invite</div></button>
Full XPath:
/html/body/div[1]/div[2]/div/div[1]/div/div/div/section/div/button
I've attached a discord invite link at the bottom of the page as an example.
Any help would be much appreciated.
https://discord.com/invite/the1
The desired element is a dynamic element, so to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='button'] > div[class^='contents']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button/div[starts-with(#class, 'contents') and text()='Accept Invite']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Selenium send_keys doesn't send uppercase R within the input bar

I'm using selenium 4.1.0 and I'm trying to send 'CRISTIAN' in an input bar through
input_bar.send_keys('CRISTIAN')
But it shows 'CISTIAN' in the bar.
I've tried also tried:
ActionChains(driver).click(input_bar).send_keys('CRISTIAN', Keys.ENTER).perform()`
But I get the same result. I checked all the uppercase letters and I figured out that only R have this problem. Any suggestions? Does it depends on this version of Selenium?
HTML of the input bar:
<div _ngcontent-qhp-c128="" cdkdroplist="" cdkdroplistorientation="horizontal" cdkdroplistdisabled="" class="cdk-drop-list d-flex flex-1 cdk-drop-list-disabled" id="cdk-drop-list-16"><input _ngcontent-qhp-c128="" cdkdrag="" data-bp="input" class="cdk-drag comp-input mt-2 cdk-drag-disabled" placeholder="Aggiungi elemento"><!----><!----><!----><!----></div>
The desired element is a Angular element, to send a character sequence you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[id^='cdk-drop-list'][cdkdroplistorientation='horizontal'] input[class*='cdk-drag'][data-bp='input'][placeholder='Aggiungi elemento']"))).send_keys('CRISTIAN')
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#cdkdroplistorientation='horizontal' and starts-with(#id, 'cdk-drop-list')]//input[contains(#class, 'cdk-drag') and #data-bp='input'][#placeholder='Aggiungi elemento']"))).send_keys('CRISTIAN')
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Web Scraping: How to click a specific href link in selenium python?

I wanted to automate a web page using webdriver(Python) wherein the script will click on the href link directly in order to navigate to other page. Couple of details in the specific tag:
<span class = "xyz", ui-sref ="abc", href= "/letters/letters_number/">
"hdf"
<i class = 'icons'>letters</i>
</span>
To click() on a the element with the href attribute you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[href=/letters/letters_number/] i.icons"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#href='/letters/letters_number/']//i[#class='icons']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to bypass cookiebot.com uc.js by Selenium Python

I need to find a way to click on the cookie agreement button created by the javascript code that is provided by cookiebot.com such as the following example in a HTML code,
<script id="Cookiebot" src="https://consent.cookiebot.com/uc.js" data-cbid="8123486-d5f-ec" data-blockingmode="auto" type="text/javascript"></script>
I have searched the net, but there is no example showing how to do that using Selenium Python.
To click() on the element Allow all you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.cookiebot.com/en/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
Using XPATH:
driver.get("https://www.cookiebot.com/en/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Python, Selenium: how to click on javascript:void(0) element

I have an "onclick" element in a webpage,
<a href="javascript:void(0)" onclick=
"toIndexHtml('3','http://xxxxxxxxxxx/trade','0')">
<i></i>
<span></span>
trade
</a>
It is shown in the webpage as a button and I want to click on it, I tried to search for it using the following code:
driver.find_element_by_xpath("//a[contains(#onclick,'toIndexHtml')]").click()
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"/html/body/div/ul/li[3]/a"))).click()
Both are not working, please suggest if there is any other ways! Thanks in advance!
P.S.: I am using Chrome WebDriver and Chrome v64.
Your first locator looks perfect and should have worked.
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='toIndexHtml']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#onclick,'toIndexHtml')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Categories