unable to select an element using xpath using selenium - python

I'm trying to automate a process using selenium and have been able to open a webpage and click on links, however I stumbled upon a table in which a link needs to be clicked but I'm unable to select that link and getting an error. need help to select that particular element
right now this is what I've done
elem2=browser.find_elements_by_xpath('/html/body/div[3]/table/tbody/tr[1]/td[2]/div[2]/table/tbody/tr[7]/td[3]/a::text')
elem2.click()
you can see in the picture that I'm trying to access the findhtml.org link.
the error that I get is
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression /html/body/div[3]/table/tbody/tr[1]/td[2]/div[2]/table/tbody/tr[7]/td[3]/a::text because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '/html/body/div[3]/table/tbody/tr[1]/td[2]/div[2]/table/tbody/tr[7]/td[3]/a::text' is not a valid XPath expression.
(Session info: chrome=81.0.4044.113)

First you have to switch to the iframe
Example:
frame = browser.find_elements_by_xpath('//iframe[contains(#src, \'hbx.media.net\')]')
browser.switch_to.frame(frame)
Now you can click
link = browser.find_elements_by_xpath('//a[contains(#href, \'http://www.findhtml.org\')]')
link.click()

To click on the specific link try below code.
Induce WebDriverWait() and presence_of_element_located() and following xpath.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://publicrecords.netronline.com/state/IL/county/dupage")
element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//a[#href='http://www.dupageco.org/PropertyInfo/PropertyLookUp.aspx' and contains(.,'Go to Data')]")))
element.location_once_scrolled_into_view
element.click()
Please note the element is not inside any iframe

browser.get('https://publicrecords.netronline.com/state/IL/county/dupage')
wait = WebDriverWait(browser, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//td[contains(text(),'DuPage Supervisor of Assessments')]//following-sibling::td[2]//a"))).click()
output:
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

Related

Invalid argument: 'using' must be a string extracting image source within an iframe

I'm new about using python and selenium and I'm trying to get the image source from a website. I'm actually finding elements by the tag name, but I don't know if it's the right way.
Anyway it gives me an error:
InvalidArgumentException: Message: invalid argument: 'using' must be a string
Under this there's the code I wrote
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
PATH = Service("/Users/fscozano/documenti/chromedriver-2.exe")
print("setup")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://apod.nasa.gov/apod/random_apod.html")
tag = driver.find_element((By.TAG_NAME, "b")).getText()
print(tag)
driver.quit()
The error is in:
tag = driver.find_element((By.TAG_NAME, "b")).getText()
You can personally inspect the website because I really don't know how to solve this
Every type of help is appreciated :)
The image source is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following Locator Strategies:
driver.get("https://apod.nasa.gov/apod/random_apod.html")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://apod.nasa.gov/apod']")))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[normalize-space()='Astronomy Picture of the Day']//following::p[2]//a/img"))).get_attribute("src"))
Console Output:
https://apod.nasa.gov/apod/image/2111/MACSJ0138_Hubble_1080.jpg
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
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
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

Selenium selenium.common.exceptions.NoSuchElementException error sending text to an element within iframe

im new to python and recently got into selenium , i made small projects for linkedin or twitter with it from a tutorial but now i wanted to do smth for my work(finance) and my problem is:
On this website: https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui
When i try to find a element by any selector(name xpath css selectors etc) it tells me there is no such element
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from selenium.common.exceptions import NoSuchElementException
URL = 'https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui'
s = Service('My chromedriver path')
driver = webdriver.Chrome(service = s)
driver.get(URL)
driver.maximize_window()
time.sleep(3)
cui_entry = driver.find_element(By.CSS_SELECTOR,'.col-sm-4 p input')
cui_entry.send_keys('23484xxx')
What i want it to do is to write this code where it says Introduceti codul unic de identificare (numeric): but it seems like im doing something wrong
To send a character sequence to the Enter the unique identification code (numeric) field as the elements are within an iframe so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#_com_liferay_iframe_web_portlet_IFramePortlet_INSTANCE_AAFALwmoH3eD_iframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='cod']"))).send_keys("23484")
Using XPATH:
driver.get("https://mfinante.gov.ro/domenii/informatii-contribuabili/persoane-juridice/info-pj-selectie-dupa-cui")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='_com_liferay_iframe_web_portlet_IFramePortlet_INSTANCE_AAFALwmoH3eD_iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='cod']"))).send_keys("23484")
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:
Reference
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
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

Can't locate a button using selenium to press on it

I've created a script in python using selenium to click on a like button available in a webpage. I've used xpath in order to locate that button and I think I've used it correctly. However, the script doesn't seem to find that button and as a results it throws TimeoutException error pointing at the very line containing the xpath.
As it is not possible to hit that like button without logging in, I expect the script to get the relevant html connected to that button so that I understand I could locate it correctly.
I've tried with:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = "https://www.instagram.com/p/CBi_eIuAwbG/"
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver,10)
driver.get(link)
item = wait.until(EC.visibility_of_element_located((By.XPATH,"//button[./svg[#aria-label='Like']]")))
print(item.get_attribute("innerHTML"))
How can I locate that like button visible as heart symbol using selenium?
To click on Like Button induce WebDriverWait() and wait for visibility_of_element_located() and below xpath.
Then scroll the element into view and click.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get("https://www.instagram.com/p/CBi_eIuAwbG/")
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//button[.//*[name()='svg' and #aria-label='Like']]")))
element.location_once_scrolled_into_view
element.click()
You can do it like this
likeSVG = driver.find_element(By.CSS_SELECTOR, 'svg[aria-label="Like"]')
likeBtn = likeSVG.find_element(By.XPATH, './..')
likeBtn.click()
likeBtn is equal to the parent of the likeSVG div as you can use XPATH similar to file navigation commands in a CLI.
Try using the .find_element_by_xpath(xPath) method (Uses full xpath):
likeXPATH = "/html/body/div[1]/section/main/div/div[1]/article/div[2]/section[1]/span[1]/button"
likeElement = driver.find_element_by_xpath(likeXPATH)
likeElement.click()

Selenium find_element_by_xpath not working for instagram

I am trying to web scrape Instagram using Python and Selenium. I have had many issues regarding locating the elements but somehow managed to pull through when I tried enough xpaths. But when I try to web scrape Donald Trump's following list (I want this to work for ANY USER'S following list/page), it just doesn't work. Here is the error it's throwing:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="f3b066159b38864"]/div/div/a"}
I get the xpaths by right clicking on the element using Google Chrome's inspect feature. If anyone needs me to post the full code I'd be happy to do so.
Try below xpath::
wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'laraleatrump')]")))
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Working solution :
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver.get("https://www.instagram.com/realdonaldtrump/")
driver.maximize_window()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'following')]"))).click()
peoples = wait.until(
EC.visibility_of_all_elements_located((By.XPATH, "//div[#role='dialog']//div[contains(#class,'PZuss')]//a")))
for peoplename in peoples:
print peoplename.text

How to fix "SyntaxError: Failed to execute 'evaluate' on 'Document':"?

I am trying to copy the xpath of an element, but it keeps saying that the xpath is incorrect
Here is the code I have done:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.bol.com/nl/v/bananago-nl/1067588/")
x = driver.find_element_by_xpath("//*[#id=js_overview']/ul/li[2]/div/div[2]/p/text()[2]")
print(x)
The element I am trying to copy is the text that says "12 beoordelingen". Let me know if you need any more information
You are missing the initial single quote in 'js_overview'.
To get the Text 12 beoordelingen you need to induce WebDriverWait and element_to_be_clickable() to get the element and then
Induce JavaScripts Exceutor execute_script() and get the lastChild textContent .
Try below code.
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 import webdriver
driver = webdriver.Chrome()
driver.get("https://www.bol.com/nl/v/bananago-nl/1067588/")
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[#id='js_overview']//div[#class='media__body']/p")))
print(driver.execute_script('return arguments[0].lastChild.textContent;', element))

Categories