How to click the page link with selenium python - python

My error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"entrar"}
I have the html below:
<a role="menuitem" href="/login" class="_yce4s5">
<div class="_hgs47m">
<div class="_10ejfg4u">
<div>
Entrar
</div>
</div>
</div>
</a>
I click on the then link entrar, the following code in selenium with python:
driver.implicitly_wait(10)
element = driver.find_element_by_link_text('Entrar')
element.click()
But It raises this NoSuchElementException.

Did you try to use Upper case for the first letter? like 'Entrar' instead of 'entrar'

You might try driver.find_element_by_partial_link_text('entrar') because there is probably other text in your link. I like getting element by xpath. It's pretty easy. If you have Firefox just get the add-on xpath finder to find the xpath to any element on a web page.

To click the page link with text as Entrar as the element is a dynamic element ou have to induce WebDriverWait for the desired element to be clickable and you can use either of the following solution:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[role='menuitem'][href='/login'] > div > div > div"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#role='menuitem' and #href='/login']//div[normalize-space()='Entrar']"))).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

Type in a textbox via python: unable to locate element

I'm trying to locate an element using python selenium, and have the html below:
<input class="form-control" type="text" placeholder="University Search">
I couldn't locate where to type what I want to type.
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path=r"D:\Python\Lib\site-packages\selenium\chromedriver.exe")
driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')
#<input class="form-control" type="text" placeholder="University Search">
text_area = driver.find_element_by_name('University Search')
text_area.send_keys("oxford university")
You are attempting to use find_element_by_name and yet this element has no name attribute defined. You need to look for the element with the specific placeholder attribute you are interested in - you can use find_element_by_xpath for this:
text_area = driver.find_element_by_xpath("//input[#placeholder='University Search']")
Also aside: When I open my browser, I don't see an element with "University Search" in the placeholder, only a search bar with "Site Search" -- but this might be a regional and/or browser difference.
Make sure you wait for the page to load using webdriver waits,click the popup and then proceed to target the element to send keys to.
driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')
driver.maximize_window()
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='OK, I agree']"))).click()
text_area=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR ,"td.uni-search.uni.sorting_disabled > div > input")))
text_area.send_keys("oxford university")
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Element to target
<input class="form-control" type="text" placeholder="University Search">
To send a character sequence to the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("input.form-control[placeholder='University search']").send_keys("oxford university")
Using xpath:
driver.find_element_by_xpath("//input[#class='form-control' and #placeholder='University search']").send_keys("oxford university")
Ideally, to send a character sequence to 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:
driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control[placeholder='University search']"))).send_keys("oxford university")
Using XPATH:
driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control' and #placeholder='University search']"))).send_keys("oxford university")
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
Browser Snapshot:
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
Very close, try the XPath when all else fails:
text_area = driver.find_element_by_xpath("//*[#id='qs-rankings']/thead/tr[3]/td[2]/div/input")
You can copy the full/relative XPath to clipboard if you're inspecting the webpage's html.

selenium.common.exceptions.NoSuchElementException: Message: no such element error invoking click on followers link within Instagram with Selenium

I am building an Instagram bot using selenium, and I want to click on my followers to open the complete list of followers. The problem is, I always get the following error message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
I tried locating the element by its Xpath, class name and CSS selector but neither of those worked. I found a post that recommended the following method, which also did not work:
driver.find_element_by_partial_link_text("followers").click()
The strangest part is, once the DevTools are open in the same browser, all of the methods I used worked, i.e. opened my list of followers. I am using Microsoft Edge Version 85.0.564.51 btw.
What am I missing here? If needed, I can provide more code.
1st method
The followers element in HTML code is an hyperlink tag :
<a class="-nal3 " href="/[PROFILE_ID]/followers/" tabindex="0"><span class="g47SY " title="237">237</span> followers</a>
You can actually go to this URL :
driver.get("https://www.instagram.com/[PROFILE_ID]/followers/")
2nd method
Your method should work with this xpath instead
driver.find_element_by_xpath("//a[contains(.,'followers')]").click()
On instagram the link of followers is as follows:
<a class="-nal3 " href="/PCy/followers/" tabindex="0">
<span class="g47SY " title="999">999</span>
" followers"
</a>
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[href$='/followers/'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'followers')]"))).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

receiving error message "no such element: Unable to locate element" when trying to find an element with xpath

Access to a hosted MariaDB using a connector is not allowed by the provider. I therefore try to export some tables using a Python script with Selenium. I do not manage to find / click the export button of phpMyAdmin.
I try to locate the button using its XPATH, obtained with the Chrome browser.
I updated Chrome, the driver, Selenium to the latest versions. Attempted to make the driver wait:
(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='navigationbar']/ul[#id='topmenu']//li//img[#title='Exporteren']"))).click())
The problem is that for some reason, the button cannot be found by the driver.
I tried to search by xpath, class, css, … without success.
I do not find any frame in the html code.
Below some html code (that seems to get interpreted in the question...)
HTML:
<div class="navigationbar"><ul id="topmenu" class="resizable-menu">
<li>
<a href="server_status.php" class="tab">
<img src="themes/dot.gif" title="Status" alt="Status" class="icon ic_s_status" /> Status
</a>
</li>
<li>
<a href="server_export.php" class="tab">
<img src="themes/dot.gif" title="Exporteren" alt="Exporteren" class="icon ic_b_export" /> Exporteren
</a>
</li>
<li>
Code trials:
python
btnexp = driver.find_element_by_xpath("//*[#id='topmenu']/li[4]/a/img")
btnexp.click()
Error message:
no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='topmenu']/li[4]/a/img"}
Activation of the most recent window: driver.switch_to_window(driver.window_handles[-1])
To click() on the element with text as Exporteren you have 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.navigationbar > ul#topmenu li img[title='Exporteren']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='navigationbar']/ul[#id='topmenu']//li//img[#title='Exporteren']"))).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
You can find a couple of relevant discussions in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
Have you tried locating the element by Class Name?
content = driver.find_element_by_class_name('icon ic_s_status')
content = driver.find_element_by_class_name('icon ic_b_export')

Unable to locate element: while clicking a button through Selenium and Python

I want to click on a button (event) using Selenium on Python and the button code is:
<input id="workbenchLst:j_id_id509" name="workbenchLst:j_id_id509" onclick="A4J.AJAX.Submit('workbenchLst',event,{'similarityGroupingId':'workbenchLst:j_id_id509','parameters':{'ajaxSingle':'workbenchLst:j_id_id509','workbenchLst:j_id_id509':'workbenchLst:j_id_id509'} ,'containerId':'j_id_id1'} );return false;" value="Add" type="button" autocomplete="off">
My code:
driver.find_element_by_id("workbenchLst:j_id_id509").click()#add
and it isn't working, the error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="workbenchLst:j_id_id509"]
Check for the iframe/frame in your page first, if there is a frame/iframe then you need to switch the frame first like below :
driver.switch_to_frame(driver.find_element_by_id("iframeid"));
You can try clicking on the element using below XPath:
element = driver.find_element_by_xpath("//input[contains(#id, 'workbenchLst') and #value='Add']");
element.click();
Or you can try using the JavaScript Executor like below :
element = driver.find_element_by_xpath("//input[contains(#id, 'workbenchLst') and #value='Add']");
driver.execute_script("arguments[0].click();", element);
Still not working then try to give some delay, import sleep from time like below :
from time import sleep
driver.switch_to_frame(driver.find_element_by_id("iframeid"));
sleep(5);
element = driver.find_element_by_xpath("(//input[contains(#id, 'workbenchLst') and #value='Add'])[2]");
element.click();
I hope it works...
The desired element is a dynamic element so to locate the element you have 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[id^='workbenchLst:'][name^='workbenchLst:'][value='Add']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[starts-with(#id,'workbenchLst:') and starts-with(#name,'workbenchLst:')][#value='Add']"))).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 click javascript:void(0) link with selenium?

I am using selenium and chrome to automate the clicks of a page with python.
I am getting stuck not being able to click on the following href link:
<a href="javascript:void(0)" class="addSuppData-trigger pts" data-target="edit_3-1" style="padding-right:6px;float:right;">
<i class="material-icons black-text tiny-small">edit</i></a>
I have tried using xpath, css, and linktext to no avail.
sample code:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="row-group-1"]/td[1]/div/div[2]/a/i' ))).click()
The goal is to click the pen, then select the item from the drop down.
screen shot of button
The second highlight line is the pen.
html tree
The element seems to be a dynamic element and to click on it you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.addSuppData-trigger.pts[data-target^='edit_']>i.material-icons.black-text.tiny-small"))).click()
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='addSuppData-trigger pts' and starts-with(#data-target, 'edit_')]/i[#class='material-icons black-text tiny-small' and contains(., 'edit')]"))).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

Resources