NoSuchElementException when using Xpath - python

So I am trying to access the website and I want the program to apply some filters for me via click. Although when I try to use the function find_element_by_xpath it says that this function is depreciated. Can someone help me ? Also the website is kind of weird because it is more like a dashboard than a website and so I dont know how precise xpath will work. thank you all so much in advance

The Solution For this problem is :
using the new feature find_element()
include this import too
from selenium.webdriver.common.by import By
this is an example :
button = driver.find_element_by_class_name("quiz_button")
button = driver.find_element(By.CLASS_NAME, "quiz_button")
please see this selenium documentation : https://www.selenium.dev/selenium/docs/api/py/api.html

driver.find_element_by_xpath('//*[#id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
Due to Depreciation Warning use the updated By method
driver.find_element(By.XPATH,'//*[#id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
From there due to page loading.
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="filtro-04"]/div/article/div[1]/div/div/qv-filterpane/div/div/div/div[2]/span'))).click()
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Related

How can I send text to text editor with Python Selenium?

I want to send a text to the post editor with Selenium, but I have a problem with xpaths, how can I solve it?
Error code:
DeprecationWarning: find_element_by_* commands are deprecated. Please
use find_element() instead element
= driver.find_element_by_xpath("/html/body").send_keys(DETAIL)
My Code
element = driver.find_element_by_xpath("/html/body").send_keys(DETAIL)
The part where I want to send the text
driver.find_element_by_xpath("/html/body").send_keys(DETAIL)
should be due to depreciate also import by.
driver.find_element(By.CSS_SELECTOR,".cke_editable.cke_editable_themed.cke_contents_ltr").send_keys(DETAIL)
You also have an iframe so switch to that first.
WebDriverWait(driver,30).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,".cke_wysiwyg_frame.cke_reset")))
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

ActionChains double_click() method does not performs the double click using Selenium and Python

I have the following web page
.
I'm trying to double click the "Blocked_IPs" text.
This is the code that interacts with it:
blocked_ips = driver.find_elements_by_xpath('//td[contains(.,"Blocked_IPs")]')
print(len(blocked_ips), blocked_ips)
action = ActionChains(driver)
action.double_click(blocked_ips[0])
Problem is, it just doesn't seem to double click it. When I do it manually, it works. When I execute the code, it doesn't. There's only one occurance of the word "Blocked_IPs". This is the output in the terminal:
1 [<selenium.webdriver.remote.webelement.WebElement (session="82b277a5f85cbb202f5cd57c0b800f3b", element="530b1a15-a190-401c-8495-921777f8fa84")>]
Does anyone happen to know why it's not working? How can I test it? Thanks ahead!
You need to add perform() to make all the ActionChains happen as follows:
action.double_click(blocked_ips[0]).perform()
Ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
ActionChains(driver).double_click(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[contains(.,"Blocked_IPs")]")))).perform()
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
from selenium.webdriver.common.action_chains import ActionChains

Python Selenium Value select

Can someone help me please, I have been stuck in one place in my script for 2 days .
A menu should be clicked and an option should be selected, unfortunately it does not work, I have started about 50 code attempts, unfortunately without success
This is the html text for which I need a Selenium code solution
<select class="categoryAttributesSelect form-control"><option value="-1">Bitte Zustand auswählen...</option><option value="22">Neu</option><option value="2546">Neuwertig</option><option value="23">Gebraucht</option><option value="24">Defekt</option></select>
I need a code that selects the option (new) for me
This not Working..
minimenu1 = Select(browser.find_element_by_xpath('/html/body/div[4]/div/div/form/div[1]/div[1]/div[2]/div[2]/div[3]/div[2]/div[4]/div[1]/div[2]/select'))
minimenu1.select_by_visible_text("Neu")
minimenu1.select_by_value("22")
See if this works.
minimenu1 = Select(browser.find_element_by_xpath("//select[contains(#class,'categoryAttributesSelect')]"))
minimenu1.select_by_visible_text("Neu")
You can use the below code :
wait = WebDriverWait(driver, 10)
menu = Select(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "select.categoryAttributesSelect.form-control"))))
menu.select_by_visible_text('Neu')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Python + Selenium -- Unable to locate image element (Can't click image)

I'm trying to create a macro that opens up all my online classes on Chrome (cause logging in is annoying, especially if you have to do it 8 times every morning).
BTW, I use Python 3.8, Chrome 81.0.4044.122, and the latest version of Selenium.
Until now, I clicked button using:
driver = webdriver.Chrome()
driver.find_element_by_xpath("PATH_OF_ELEMENT").click()
And then I find a login button that has a image instead of text.
I tried XPath, CSS Selector, id, name, the link of text, ActionChains (move_to) nothing works.
Here's the HTML:
here click me please.
The button I'm trying to press is the one with the tag a.
I spent 30 minutes googling about this and all I found was Stack Overflow questions from 6 years ago. They suggested I use WebDriverWait or change the frame. None of them worked (I might have made a mistake). I'm new to Selenium so please be kind and explain hard stuff.
How can I find the correct XPath of an image button and click it?
driver.find_element_by_css_selector('.nice-select').click()
driver.find_element_by_xpath("/html/body/div1/div[3]/div/div/div/div/div/div2/div1/div/ul/li2").click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div2/div/span').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div2/div/ul/li[19]').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[3]/div/span').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[3]/div/ul/li[3]').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[4]/div/span').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[4]/div/ul/li[3]').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[5]/a').click()
Try the following CSS Selector:
.my_menu>a
Code should look like:
driver.find_element_by_css_selector(".my_menu>a").click()
Also, try to locate the element with explicit wait:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".my_menu>a"))).click()
I have tested with the following code block (as result will be displayed popup with 2 options):
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get("https://oc31.ebssw.kr/onlineClass/search/onlineClassSearchView.do?schulCcode=00898&schCssTyp=online_mid")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".my_menu>a"))).click()
time.sleep(5)
I hope it helps you!

xpath for first post on instagram profile not working (python, selenium, chromedriver)

I am trying to click on the first post after navigating to any Instagram profile. I looked at the xpath of the first post of multiple Instagram user's profiles and they all seem to be the same. Here is an example of messi's profile.
Here is my attempt with using chromedriver with python to click on Messi's first post. I have already navigated to https://www.instagram.com/leomessi/, which is Messi's profile.
first_post_elem_click = driver.find_element_by_path('//*[#id="react-root"]/section/main/div/div[4]/article/div[1]/div/div[1]/div[1]/a/div').click()
However, the first post is not being clicked on. Would greatly appreciate any help.
Please check below solution,
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Browser = webdriver.Chrome(executable_path=r"chromedriver.exe")
Browser.get("https://www.instagram.com/leomessi/")
WebDriverWait(Browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//body//div[contains(#class,'_2z6nI')]//div//div//div[1]//div[1]//a[1]//div[1]//div[2]"))).click()
Instead of using the absolute xpath, you should be using relative xpath.
You can click on the first post using the below command(Have applied Explicit wait as well):
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "(//div[#class='Nnq7C weEfm']//img)[1]"))).click()
You need 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
I have just checked this in Firefox: $x('//*[#id="react-root"]/section/main/div/descendant::article/descendant::a[1]'). That should give you what you want, I think.

Categories