Unable to click on link using python and selenium - python-3.x

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# go to website
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=chrome_options)
action = webdriver.ActionChains(driver)
driver.get('https://www.clinicalkey.com/#!/browse/book/3-s2.0-C2016100010X')
# look for "login" and click
loginclick = driver.find_element_by_xpath("//*[#id='header']/div[3]/ol/li[3]/a/span").click()
How come I'm not able to click on the login section on top right after navigating to the website given? I get an error:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='header']/div[3]/ol/li[3]/a/span"}
(Session info: chrome=85.0.4183.83)
File "C:\python\download.py", line 18, in <module>
loginclick = driver.find_element_by_xpath("//*[#id='header']/div[3]/ol/li[3]/a/span").click()
Thank you !

Two reason:
1: Wait for element to load
2: As element is not visible on page use java script to click
driver.get('https://www.clinicalkey.com/#!/browse/book/3-s2.0-C2016100010X')
# look for "login" and click
loginclick=WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[#id='header']/div[3]/ol/li[3]/a/span")))
driver.execute_script("arguments[0].click();", loginclick)
Output:

Related

Python traceback occurs when trying to located element by xpath

Currently have a script which logs me into our company website, clicks an element which loads a table of data i'm trying to collect, the code that provides traceback is
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//tr[position()=8]"}
In inspect element that xpath shows that there's 2 relative values (i'm looking for the 1st one)
xpathBeingShownInDOM
and here is the traceback that python gives me after running script.
traceback error
Here goes the full code minus my login credentials:
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
browser = webdriver.Chrome(options=options)
browser.get('https://portal.o2pdelta.processwaregroup.com/O2PIntranet/O2PIntranet.aspx')
elem = browser.find_elements_by_xpath("//input[#name='tbUsername' and #id='tbUsername']")[0]
elem.send_keys('MyLogin')
elem2 = browser.find_elements_by_xpath("//input[#name='tbPassword' and #id='tbPassword']")[0]
elem2.send_keys('MyPassword' + Keys.RETURN)
#browser.implicitly_wait(5)
time.sleep(3)
elem3 = browser.find_element_by_xpath("//div[#class='col-md-4' and #id='TrIcon_OPD09']")
elem3.click()
time.sleep(3)
elem4 = browser.find_element_by_xpath("//tr[position()=8]")```

Scroll to right in Tradingview chart using python

I am trying to scroll right using selenium python in TradingView
My Code:
driver.find_element_by_xpath("//div[#class='control-bar__btn control-bar__btn--move-right apply-common-tooltip']").click()
It throws an error:
selenium.common.exceptions.ElementNotInteractableException: Message:
element not interactable
Try the below code -
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 60)
action = ActionChains(driver)
driver.maximize_window()
driver.get('https://www.tradingview.com/chart/')
try:
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(#class,\"closeIcon\")]"))).click()
except:
pass
RightScroll = wait.until(EC.presence_of_element_located((By.XPATH,"//div[contains(#class,\"btn--move-right\")]")))
for i in range(10):
time.sleep(2)
print(i)
action.move_to_element(RightScroll).click().perform()
If it solves your problem then please mark it as answer. Do let me know if you have any query.
There is a message window displayed when you first load the page. You have to close it first (to get the right arrow container visible) if you are not doing it by
driver.findElement(By.xpath(".//span[contains(#class,'closeIcon')]")).click();
driver.findElement(By.xpath(".//td[#class='chart-markup-table time-axis']")).click();//To get the right arrow container visible
driver.findElement(By.xpath(".//div[#class='control-bar__btn control-bar__btn--move-right apply-common-tooltip']")).click();

How to hit the Next button from Python using Selenium?

I am trying to work with the following site using Python.
Please advise how can I hit the Next button:
I have tried the following:
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.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(executable_path = r'C:/chromedriver.exe')
driver.maximize_window()
wait = WebDriverWait(driver,40)
driver.get('https://www.okcupid.com/login')
driver.execute_script('arguments[0].click();',wait.until(EC.element_to_be_clickable((By.ID, 'username'))))
wait.until(EC.element_to_be_clickable((By.ID, 'username'))).send_keys("abc")
driver.execute_script('arguments[0].click();',wait.until(EC.element_to_be_clickable((By.ID, 'username'))))
wait.until(EC.element_to_be_clickable((By.ID, 'password'))).send_keys("123")
# Find login button
login_button = driver.find_element_by_name('Next')
# Click login
login_button.click()
But got an error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="Next"]"}
Name is a an attribute on a webelement. The next button has no Name attribute.
You can try this xpath:
//input[#value='Next']
login_button = driver.find_element_by_xpath('//input[#value="Next"]')

How to click element using selenium web driver(Python)

I am trying to click an element using selenium chromedriver by the Use of ID of an element to click.
I want to Click Year '2020" from the following webpage: 'https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan'
I tried with the below code.
driver = webdriver.Chrome(executable_path=ChromeDriver_Path, options = options)
driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')
Id = "ctl00_ContentPlaceHolder1_gvMajorIncident_ct123_lbtnYear" ### Id of an Element 2020
wait = WebDriverWait(driver, 20) ##Wait for 20 seconds
element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
time.sleep(10)
but unfortunately this gives an Error as below:
element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
File "C:\Users\Pavan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Please anyone help me on this... Thanks;
I don't know if your imports were correct. Also, your code doesn't scroll to the bottom of the page. Use this.
import os
from selenium.webdriver.support import expected_conditions as EC
import time
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
driver = webdriver.Chrome(executable_path='driver path')
driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')
driver.maximize_window()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
element = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="ctl00_ContentPlaceHolder1_gvMajorIncident_ctl23_lbtnYear"]')))
element.click()

Python - Selenium XPATH query mismatch

I've tested an XPATH query using Chrome SelAssist extension and it works pretty fine.
The syntax is "//*[#id="fbPhotoSnowliftTimestamp"]/a/abbr":
I've started to write a python code to detect such element:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_profile = r"C:\Users\XXX\AppData\Local\Google\Chrome\User Data"
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=' + chrome_profile)
w = webdriver.Chrome(executable_path="C:\\Projects\\selenium\\chromedriver.exe", chrome_options=options)
w.get('https://website.com')
test = w.find_element(By.XPATH, "//*[#id=\"fbPhotoSnowliftTimestamp\"]/a/abbr")
Page is loaded fine, and it's exactly the same one I've manually tested.
Unfortunately I continue to retrieve this error:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="fbPhotoSnowliftTimestamp"]/a/abbr"}
(Session info: chrome=80.0.3987.149)
I cannot figure out what I'm doing wrong.
Thx for any suggestion.
The page might not be loading as quickly as expected. A timeout exception will be thrown if the xpath is not found using below. Try:
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
chrome_profile = r"C:\Users\XXX\AppData\Local\Google\Chrome\User Data"
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=' + chrome_profile)
w = webdriver.Chrome(executable_path="C:\\Projects\\selenium\\chromedriver.exe", chrome_options=options)
w.get('https://website.com')
test = WebDriverWait(w, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[#id=\"fbPhotoSnowliftTimestamp\"]/a/abbr")))
#w.find_element(By.XPATH, "//*[#id=\"fbPhotoSnowliftTimestamp\"]/a/abbr")

Resources