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

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

Related

NoSuchElementException when using Xpath

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

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

Clicking Apply LinkedIn button using selenium

I am trying to click on the Apply button on linkedIn but, having a hard time in doing so. I tried to target the id but I noticed that the id always changes when the page reloads.
I've tried:
browser.find_element_by_xpath("//span[text() = 'Apply']").click() and browser.find_element_by_xpath("//button[#type = 'Apply']").click() but it doesn't work.
Use either of the xpath.
browser.find_element_by_xpath("//span[normalize-space(.)='Apply']").click()
OR
browser.find_element_by_xpath("//span[contains(.,'Apply')]").click()
UPADTE:
It seems synchronization issue induce WebDriverWait() and wait for element_to_be_clickable()
WebDriverWait(driver,15).until(EC.element_to_be_clickable((By.XPATH,"//span[normalize-space(.)='Apply']"))).click()
You need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

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.

How to find element by xpath in Selenium Python?

I am trying to use Selenium in Python to click on a link to a report on a web page. I have it working up to the point where it opens the page that the report is on, but I am having trouble actually clicking that specific report.
The page has a list of reports all with the same class. This is what I get when I inspect that specific report for example:
<a class="rpt" href="reportConfigRedirect.asp?action=filter&rc_id=181786&letter=">Run with new filters</a>
I have tried:
driver.find_element_by_xpath("xpath")
and this doesn't seem to work, it doesn't do anything once it gets to that page with the report.
Induce WebdriverWait and element_to_be_clickable.Use the following Xpath.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[#class='rpt'][contains(.,'Run with new filters')]")))
element.click()
If unable to click using Webdriver try use javascript executor.
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[#class='rpt'][contains(.,'Run with new filters')]")))
driver.execute_script("arguments[0].click();",element)
EDITED
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(.,'Run with new filters')]")))
driver.execute_script("arguments[0].click();",element)
OR
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(#hef,'reportConfigRedirect.asp?action=filter')][contains(.,'Run with new filters')]")))
driver.execute_script("arguments[0].click();",element)

Categories