Unable to locate element XPath (Selenium) - python

I am trying to use this XPath in Selenium with Python but it's generating an error: 'Message: no such element: Unable to locate element':
driver.find_element(By.XPATH, '//div[contains (#class, "files-list-grid-view")]/div[1][contains (#class, "folder")]').click()
This code works in Dev Tools with $x().
I need a marked element.

If the dataref node value is unique and it is not changing dynamically then you can use the below xpath :
element = driver.find_element_by_xpath("//div[#dataref='folder-0']")
If you are still getting an error then try to give some delay before locating it like below :
from time import sleep
sleep(3)
element = driver.find_element_by_xpath("//div[#dataref='folder-0']")
# And perform some action here
Still it doesn't work then check for frame or iframe using //iframe or //frame locators and if there are any matches and the locator that you are trying to locate is in some frame then switch to it using the below line and try to run your code again :
from time import sleep
# Switch to corresponding frame
driver.switch_to_frame("frame locator")
# Wait for sometime
sleep(3)
# Try to find an element
element = driver.find_element_by_xpath("//div[#dataref='folder-0']")
# And perform some action here
I hope it works...

Related

Selenium Python Error: "stale element reference: element is not attached to the page document"

HTML Image
I'm using selenium to get some information from a site, but the links that I need to click on require double clicking. Here's the code (I've included an attachment of the image of the HTML)
PATH = "C:\Program Files (x86)\msedgedriver.exe"
driver = webdriver.Edge(PATH)
action = ActionChains(driver)
from selenium import webdriver
from selenium.webdriver import ActionChains
frame = search_by_XPATH('//[#id="frmMain"]/table/tbody/tr/td[2]/div[2]').get_attribute('id').replace("_Div","_Frame")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, frame)))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"fraTree_Frame")))
rows = driver.find_elements(By.XPATH,'//*[#id="ctl00_treChart_RadTreeView"]/ul/li/div/span[2]')
for row in rows:
# row.click()
action.double_click(row).perform()
sleep(1)
I'm accessing different pages to get the links, and although each page has the same content the "id" of the frame changes with each new page I access. So that's why I use the 'get_attribute' function to get the id of the frame of the page.
I'm using action chains right now to perform the double click, but every time I do I get the stale element reference: element is not attached to the page document error. I know it's not an issue with the XPATH I'm using to access the element because I can perform a element.click() function on it just fine and the element receives the click. Nothing changes after the first click except the element gets highlighted. The element's xpath doesn't change and its still on the page. I also tried doing the action.click().perform() on the element but that produces the same error.
I've tried clicking on other parts of the element that could receive the click (the div, the span2,etc) but I always get the error. The 'span2' is the part that holds the text of the link (although its not an 'a' tag so its not really a link in that sense.)
I don't understand why the element would be able to receive the click for the .click() function but then be unable to receive the double click. I'm using the double_click() function on another element previously in my code and that works as expected.

How to select a g-menu-item in selenium (Python)

I am trying to find news articles related to different web searches within a custom time frame. If someone knows a better way to do this than with selenium then please do tell. I tried to use API's but they are all expensive.
My Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r".\chromedriver.exe")
driver.get("https://www.google.com")
cookie_button = driver.find_element_by_id('L2AGLb')
cookie_button.click()
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("Taylor Swift")
search_bar.send_keys(Keys.RETURN)
news_button = driver.find_element_by_css_selector('[data-hveid="CAEQAw"]')
news_button.click()
tools_button = driver.find_element_by_id('hdtb-tls')
tools_button.click()
recent_button = driver.find_element_by_class_name('KTBKoe')
recent_button.click()
custom_range_button = dashboards_button = driver.find_elements_by_tag_name('g-menu-item')[6]
custom_range_button.click()
driver.close()
I am trying to click this button:
I have tried to select, the span, all parent divs and now the g-menu-item itself, in every case I have received the error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I understand this can happen when the element is not visible or can not be clicked but I also tried adding in sleep(2) between each operation, and I can see in the browser that selenium opens that it gets to the point in the image I have sent and then can't find the custom range button ??
In short:
Do you know how I can click this custom range button,
Or do you know how to scrape a lot of news articles from specific dates (without expensive API's)
Those g-menu are special type of tag,
you can locate them using :
//*[name()='g-menu-item']
this xpath will point to all the nodes.
further, I see that you are interested in 6th item.
(//*[name()='g-menu-item'])[6]
in code :
custom_range_button = driver.find_element_by_xpath("(//*[name()='g-menu-item'])[6]")
custom_range_button.click()

How to click on the first google search result with python selenium?

I am trying to write program that will click on the first google search link that appears. My code is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get("https://www.google.com/")
search = driver.find_element_by_name("q")
search.clear()
search.send_keys("bee movie script")
search.send_keys(Keys.RETURN)
time.sleep(3)
assert "No results found." not in driver.page_source
result = driver.find_element_by_xpath('/html/body/div[6]/div[3]/div[8]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/div[1]/a/h3')
result.click()
I've tried a variation of things for result, but the automation is unable to find the element. I copied the xpath from inspect element, but I get an error that:
NoSuchElementException: Message: Unable to locate element: /html/body/div[6]/div[3]/div[8]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/div[1]/a/h3
Am I doing this html incorrectly and how can I fix it? Thank you.
I found a solution with:
results = driver.find_elements_by_xpath('//div[#class="r"]/a/h3') # finds webresults
results[0].click(). # clicks the first one
You can use the below xpath and css to select the nth link.
xpath:
Using the index
driver.find_element_by_xpath('(//div[#class="r"]/a)[1]').click()
If you want to access the first matching element you can simply use .find_element_xpath and script will pick the first element though there are multiple elements matching with the given locator strategy be it xpath, css or anything.
driver.find_element_by_css_selector("div.r a h3").click()

How to fix error when changing drop down option

I am very new to web scraping, so I still have lots of trouble. Currently, I am trying to web scrape from https://www.enterprisetrucks.com/truckrental/en_US.html by setting the pickup time by running this code:
pickupTime = d.find_element_by_id('fldPickuptime_msdd')
pickupTime.click();
select = Select(d.find_element_by_id('fldPickuptime'))
select.select_by_value('20:00')
But I get the error saying that the element is not currently visible and may not be manipulated.
The dropdown which is present is not of the Select type, so you cannot use the Select method here. You need to click on the time using the xpath of that element directly.
You can use the xpath:
pickupTime = d.find_element_by_id("fldPickuptime_msdd")
pickupTime.click();
selectTime = d.find_element_by_xpath("//*[#id='fldPickuptime_msdd']//span[text()='8:00 PM']")
selectTime.click();
Code for JavaScriptExecutor Click:
element = driver.find_element_by_xpath("Enter the xpath here")
driver.execute_script("arguments[0].click();", element)

Unable to click invisible select python

I am trying to retrieve all possible lists from this website
Model will only open when year is selected. Similarly Make will only open when Model is selected. I want to store all combination of Year, Model and Make for learning purpose.
However I am not able to click year field only. It seems it is hidden and without this I can't go ahead with rest of code.
from selenium import webdriver
import time
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
driver = webdriver.Chrome()
driver.get("https://www.osram-americas.com/en-us/applications/automotive-lighting-systems/Pages/lrgmain.aspx")
# Switch to new window opened
driver.switch_to.window(driver.window_handles[-1])
# Close the new window
driver.close()
# Switch back to original browser (first window)
driver.switch_to.window(driver.window_handles[0])
el = driver.find_element_by_id('sbToggle_27562807')
el.click()
It gives error :-
Message: no such element: Unable to locate element: {"method":"id","selector":"sbToggle_27562807"}
Message: no such element: Unable to locate element: {"method":"id","selector":"sbToggle_27562807"}
year element is not hidden, actually your locator to locate element using find_element_by_id('sbToggle_27562807') is not correct. In this element id attribute value is dynamically changing that's why you're unable to locate this element.
Instead of id locator you should try using some different locator. I would suggest, for better way try using find_element_by_css_selector() as below :-
el = driver.find_element_by_css_selector("div#fldYear a.sbToggle")
el.click()
Use this CSS selector to get to the arrow pointing downwards on the select year dropdown
"div[id='fldYear'] > div[class='sbHolder'] > a[class='sbToggle']"
or this xpath
"//div[#id='fldYear']/div[#class='sbHolder']/a[#class='sbToggle']"
Click on this to webelement to get the options
The element IDs change on each reload of the page, you'll have to find a different way to find the dropdown.
You can always find the <a> link with the "-- Select Year --" text, for example.

Categories

Resources