Selenium Can't find element when using find_element through Python - python

Keep getting unable to locate element error message.
The first find element by xpath was fine, but the second one is giving me a hard time.
Here is my code:
import XLUtils
from selenium import webdriver
driver=webdriver.Chrome(executable_path="C:\Chrome_Driver\Chromedriver.exe")
driver.get("https://www.canada.ca/en/revenue-agency/services/e-services/e-services-businesses/payroll-deductions-online-calculator.html")
driver.find_element_by_xpath('/html/body/main/div[1]/div[7]/p/a[1]').click()
driver.find_element_by_xpath('//*[#id="welcome_button_next"]').click()

You'll need to wait a bit for the Next button to appear.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
wait = WebDriverWait(driver, 10)
e = wait.until(
EC.presence_of_element_located((By.XPATH, '//*[#id="welcome_button_next"]'))
)
e.click()

To click on the element with text as Next you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.btn.btn-primary#welcome_button_next"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='btn btn-primary' and #id="welcome_button_next"]"))).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

Close popups in Oddschecker using Selenium Python

Need to close the cookies and ad popup on this page, I know you can do it using webdriverwait and either CSS_SELECTOR or X tags but I can't find the specific tags for the buttons, as when you click 'inspect' they disappear.
Code trials:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
s=Service('C:\Program Files (x86)\chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.get(https://www.oddschecker.com/)
Any help greatly appreciated.
To click on the element OK 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.oddschecker.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='CookieBannerAcceptButton']"))).click()
Using XPATH:
driver.get("https://www.oddschecker.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='OK']"))).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 locate the link of the tag I want from a tags with the same class with Selenium

I want to go to the jobs section on LinkedIn with selenium, but it leads to the connections(networks) section with the same class name. how do i solve this problem?
My code;
jobs_sec = driver.find_element("xpath", "//a[#class='app-aware-link global-nav__primary-link']")
jobs_sec.click()
The tag reached when run;
The desired element is a dynamic element, so to click on the clickable 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.app-aware-link.global-nav__primary-link[href^='https://www.linkedin.com/jobs']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='app-aware-link global-nav__primary-link' and starts-with(#href, 'https://www.linkedin.com/jobs')]"))).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
Try:
jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".app-aware-link.global-nav__primary-link")))
jobs_sec.click()
If that doesn't work, try as an alternative:
jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#data-nnt-old-href='https://www.linkedin.com/jobs/?']")))
jobs_sec.click()
You will also need to import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Unable to locate element with id, class_name and xpath using Selenium Python

How to click this button:
I have tried the following:
sumbitbutton = driver.find_element(By.XPATH, "//div[text() = 'mt8 mb8']")
I decided to insted abandon this method and go for a nuther as it lead to a lot of other problums latter down the line
You will want something like:
button = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "inputButton.main_submit")))
button.click()
As you did not confirm the URL, I cannot test it, but it should work.
Also, do not forget to import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Generally <div> tags aren't clickable. Additionally mt8 mb8 are the classanmes, not the text.
Solution
As per the HTML:
to click on the <input> 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, "input.inputButton.main_submit[value='Submit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='inputButton.main_submit' and #value='Submit']"))).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

Trying to click a button but keep getting an error with xpath

I am trying to automate pulling credit charges into an excel sheet; I have managed to get the login working. Once I enter the website, there's a button titled "Search". I cant seem to figure out how to have that button clicked.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import time
chromedriver = "C:/Python_Ex/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
delay = 30
driver.get("https://global.americanexpress.com/activity/date-range?from=2020-05-01&to=2020-05-30")
driver.find_element_by_xpath('//*[#id="eliloUserID"]').send_keys("removed")
driver.find_element_by_xpath('//*[#id="eliloPassword"]').send_keys("removed")
driver.find_element_by_xpath('//*[#id="loginSubmit"]').click()
time.sleep(10)
#print(driver.find_elements_by_xpath('//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button'))
search_button = driver.find_elements_by_xpath('//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button')
search_button.click()
html tag is as follows
<button class="btn btn-fluid" tabindex="0" type="button"> <span>Search</span></button>
Xpath as follows
//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button
Any help is appreciated.
To click() on the <button> with text as Search using Selenium 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, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-fluid[type='button']>span"))).click()
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-fluid']/span[text()='Search']"))).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
After login submited, try to add the below code:
...
...
driver.find_element_by_xpath('//*[#id="loginSubmit"]').click()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-fluid']//span[text()='Search']"))).click()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

How to click the follow button on Instagram?

I am trying to click the follow button on Instagram using Python Selenium
https://www.instagram.com/luvly_zuby/?hl=en
I've tried the bellow code but it's not working.
#click follow
follow = driver.find_element_by_partial_link_text("Follo")
ActionChains(driver).move_to_element(follow).click().perform()
You can click on the element by using simple selenium click by finding the element using its text in the xpath and then using explicit wait on the element.
You can do it like:
follow_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Follow']")))
follow_button.click()
You need 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
This should work for you. Pass the WebElement to click(element) method.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('testUrl')
follow = driver.find_element(By.XPATH, '//button[contains(text(),'Follow')]')
webdriver.ActionChains(driver).move_to_element(follow).click(follow).perform()
Try to find the element using this css selector a.BY3EC > button and wait using .element_to_be_clickable:
follow = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.BY3EC > button')))
follow.click()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Categories