Can't get the propper Xpath to pass to Selenium with python - python

I'm stuck with a selenium scrape using jupyter. I'm trying to get the "download page data" from the bottom right corner of this page: https://polkadot.subscan.io/account?role=all&page=1
enter image description here
Also, here's the html code:
Download page data
I've tried copying the xpath and full xpath from the Google Chrome "inspect" tab, but it doesn't work.
Here's the code I used, but feel free to suggest anything else.
#Initiating Webdriver
s=Service('CHROMEDRIVER LOCATION')
op = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=s, options=op)
link = "https://polkadot.subscan.io/account?role=all&page=1"
driver.get(link)
Ingresar = driver.find_element(By.XPATH,"//*[#id='app']/main/div/div/div[5]/div/div[3]/div[1]/div/div")
Here's the error I get:
ElementClickInterceptedException: Message: element click intercepted: Element <div data-v-24af4670="" class="label align-items-center">...</div> is not clickable at point (125, 721). Other element would receive the click: <div data-v-c018c6b4="" class="banner">...</div>
Either fixing my code, or getting a new one that works with jupyer and selenium

Try this code:
url = "https://polkadot.subscan.io/account?role=all&page=1"
driver.get(url)
driver.find_element(By.XPATH, ".//*[text()='I accept']").click()
time.sleep(5)
download_btn = driver.find_element(By.XPATH, ".//*[text()='Download page data']")
driver.execute_script("arguments[0].scrollIntoView(true);", download_btn)
download_btn.click()

Related

Selenium locator only works with inspect tab open

I am trying to scrape names and odds for a project I am working on with Selenium 4, and just having an issue with the locator.
When I use the driver.find_element(By.XPATH), the XPATH I give it only seems to work when I open the inspect window on that particular page. When I close the inspect window, it gives me an NoSuchElementException: no such element: Unable to locate element: error.
Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
url = 'https://www.bet365.com.au/#/AC/B13/C1/D50/E2/F163/'
driver.get(url)
Player1 = driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[1]/div[2]/div[1]/div/div[2]/div/div[1]/div').text
Player2 = driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/div').text
Odds1 = driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div[2]/span').text
Odds2 = driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[3]/div[2]/span').text
print(f'{Player1}\t{Odds1}')
print(f'{Player2}\t{Odds2}')
Run just the section from Player1 onwards with the inspect window open and without it open. Hopefully you'll be able to replicate the issue.
I also ran
try:
if driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[1]/div[2]/div[1]/div/div[2]/div/div[1]/div').text:
print("yay")
except:
print("nay")
and it seemed to show the same situation. The element couldn't be found without the inspect window open. See attached image for where I got the XPATHs from.
Many thanks in advance!

Python Selenium .click() not working after using ActionChains

I'm making a search on Youtube, then I press a few TAB, then I press a few END via the ActionChains... Then I want to grab the results and if I find what I want, I want to click on it.
driver.get('https://www.youtube.com')
time.sleep(4)
items = driver.find_element(By.XPATH, "//input[#id='search']")
rand = random.choice(query)
items.send_keys(rand)
items.send_keys(Keys.RETURN)
time.sleep(2)
action = ActionChains(driver)
for i in range(5):
action.send_keys(Keys.TAB)
action.perform()
time.sleep(1)
for i in range(3):
action.send_keys(Keys.END)
action.perform()
time.sleep(4)
items = driver.find_elements(By.XPATH, "//div[#id='primary']//a[#id='thumbnail'][#class='yt-simple-endpoint inline-block style-scope ytd-thumbnail'][contains(#href, 'watch?v=')]")
for i in items:
if ......... :
i.click()
Usually, after using: driver.find_elements(By.XPATH.... I can do a
for i in items:
if ...... :
i.click()
But now it's not working since I've been using the action = ActionChains(driver)
I'm getting this error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I found my answer, the problem is youtube and not selenium. i.click() works, the problem was my find_elements(By.XPATH, ... youtube also has invisible elements form the home page that are included in the result page, which gave me invisible items.
once I changed my XPATH to:
//ytd-search[#class='style-scope ytd-page-manager']//div[#id='primary']//a[#id='thumbnail'][#class='yt-simple-endpoint inline-block style-scope ytd-thumbnail'][contains(#href, 'watch?v=')]")
it worked

Selenium Error: Element not interactable (coockie and other pop up)

I am trying to press a button with selenium because afterwards I need to inspect the full html of the website. This is the code that I am using:
driver = webdriver.Chrome()
driver.get('https://www.quattroruote.it/listino/audi/a4-allroad')
time.sleep(10)
html = driver.find_element_by_id('btnallestimenti')
html.click()
But I get this error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
when the page is open there are cookies and other things that shows up, is there a way to block all of them so that I can work on the html?
Thanks a lot!
As you can see the "cookies" banner is an HTML element itself and it contains a "Close" ("Chiudi") button that can be clicked.
If you inspect the page source you will find this code that relates to that button:
<button type="button" class="iubenda-cs-close-btn" tabindex="0" role="button" aria-pressed="false" style="font-size:16px!important;">Chiudi</button>
Your script needs to be modified to search the element by visible text (using the XPath) and click it to close the banner:
close_button = driver.find_element_by_xpath("//*[text()='Chiudi']")
close_button.click()
I can see that this kind of banner appears 2 times (one for cookies one for "Informativa") but once you click this one away you are redirected to the right page.
Of course you will need to test your script and adjust it to the page's behavior.
Also, be aware that every time the pages change because the devs change it your script will break and you will need to re-adjust it.
EDIT
Posting here the full code, try to use it and continue from here:
import time
from selenium.webdriver import Chrome
driver = Chrome()
driver.get("https://www.quattroruote.it/listino/audi/a4-allroad")
time.sleep(6)
driver.find_element_by_xpath("//button[text()='Accetta']").click()
time.sleep(6)
driver.switch_to.frame("promo-premium-iframe")
driver.find_element_by_xpath("//a[normalize-space()='Non sono interessato']").click()
time.sleep(6)
driver.switch_to.default_content()
driver.find_element_by_id("btnallestimenti").click()
input()
You could try to accept the cookie and proceed further, check the below lines of code.
options = Options()
options.add_argument("--disable-notifications")
driver = webdriver.Chrome(options=options,"ChromeDriver_Path")
driver.maximize_window()
driver.get('https://www.quattroruote.it/listino/audi/a4-allroad')
sleep(10)
cookie_btn = driver.find_element_by_xpath("//button[text()='Accetta']")
cookie_btn.click()
sleep(3)
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"promo-premium-iframe")))
register_btn = driver.find_element_by_xpath("//a[normalize-space()='Accedi o Registrati']")
register_btn.click()
Iframe available so just switched to the iframe, tried the to perform the registration.

Not able to click on a link. I'm getting the error 'Element is not clickable at point'

Can't click link.
I see error
ElementClickInterceptedException: Message: Element is not clickable at
point (116,32) because another element obscures it
My code:
URL = "https://lenta.com/goods-actions/weekly-products/"
driver = webdriver.Firefox()
driver.get(URL)
time.sleep(2)
# ans = driver.find_element_by_link_text("Казань") this link works OK
ans = driver.find_element_by_link_text("Санкт-Петербург") # ERROR
ans.click()
time.sleep(5)
print("go next")
driver.get(URL)
Important code doesn't work only for "Санкт-Петербург"
There are 2 text strings with a value of "Санкт-Петербур" on this page. One is in the overlay; one is in the page header. The script is trying to click the link in the header (but can't because the overlay has focus).
from selenium import webdriver
URL = "https://lenta.com/goods-actions/weekly-products/"
driver = webdriver.Chrome()
driver.get(URL)
ans = driver.find_element_by_link_text("Санкт-Петербург")
print(ans.get_attribute("class"))
#=> link current-store__link js-pick-city-toggle

Python + Selenium can't find element by XPath

I am trying to click on the "chercher" button on the left of the page (middle).
url = "https://www.fpjq.org/repertoires/repertoire-des-medias/"
driver = webdriver.Firefox()
driver.get(url)
time.sleep(2)
driver.find_element_by_xpath('//*[#id="recherche"]/input[3]').click()
However, it can't find the element. I copy pasted the XPath so I am not sure why it's not working.
Thanks.
That's because required button located inside an iframe and to be able to click it you need to switch to that iframe:
url = "https://www.fpjq.org/repertoires/repertoire-des-medias/"
driver = webdriver.Firefox()
driver.get(url)
time.sleep(2)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_xpath('//*[#id="recherche"]/input[3]').click()
Also note that using time.sleep() is not a good practice. You can try to implement Explicitwait instead

Categories