I'm trying to enter some text into a field, it works for the first time but after that, it shows this error! ElementNotInteractableException I don't know why!
here you are my code:
try:
browser.execute_script("window.scrollBy(0,600)", "")
WebDriverWait(browser, 60).until(EC.visibility_of_element_located((By.XPATH, path)))
comment = browser.find_element_by_xpath("//XPATH")
comment.send_keys(comments)
To send a character sequence to the element instead of visibility_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:
try:
browser.execute_script("window.scrollBy(0,600)", "")
WebDriverWait(browser, 60).until(EC.element_to_be_clickable((By.XPATH, path)))
comment = browser.find_element_by_xpath("//XPATH")
comment.send_keys(comments)
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
Related
So I am trying to first pull the body where the email address lies on this specific facebook page and then pull the email address plus other attributes within that body. However, I have tried every way to locate these elements using Chropath, and SelectorHub but nothing has worked. Any idea why I am unable to use these tools?
Code trials:
from ast import Return
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome("C:/Users/Carson/Desktop/chromedriver.exe")
driver.get("https://www.facebook.com/TheVillageAtGracyFarms")
driver.maximize_window()
try:
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "body._6s5d._71pn.system-fonts--body.segoe:nth-child(2) div.rq0escxv.l9j0dhe7.du4w35lb div.rq0escxv.l9j0dhe7.du4w35lb:nth-child(6) div.du4w35lb.l9j0dhe7.cbu4d94t.j83agx80 div.j83agx80.cbu4d94t.l9j0dhe7 div.j83agx80.cbu4d94t.l9j0dhe7.jgljxmt5.be9z9djy.qfz8c153 div.j83agx80.cbu4d94t.d6urw2fd.dp1hu0rb.l9j0dhe7.du4w35lb:nth-child(1) div.j83agx80.cbu4d94t.dp1hu0rb:nth-child(1) div.j83agx80.cbu4d94t.buofh1pr.dp1hu0rb.hpfvmrgz div.l9j0dhe7.dp1hu0rb.cbu4d94t.j83agx80 div.bp9cbjyn.j83agx80.cbu4d94t.d2edcug0:nth-child(4) div.rq0escxv.d2edcug0.ecyo15nh.k387qaup.r24q5c3a.hv4rvrfc.dati1w0a.tr9rh885:nth-child(2) div.rq0escxv.l9j0dhe7.du4w35lb.pfnyh3mw.gs1a9yip.j83agx80.btwxx1t3.lhclo0ds.taijpn5t.sv5sfqaa.o22cckgh.obtkqiv7.fop5sh7t div.rq0escxv.l9j0dhe7.du4w35lb.hpfvmrgz.g5gj957u.aov4n071.oi9244e8.bi6gxh9e.h676nmdw.aghb5jc5.o387gat7.g1e6inuh.fhuww2h9.rek2kq2y:nth-child(1) div.lpgh02oy:nth-child(2) div.sjgh65i0 div.j83agx80.l9j0dhe7.k4urcfbm div.rq0escxv.l9j0dhe7.du4w35lb.hybvsw6c.io0zqebd.m5lcvass.fbipl8qg.nwvqtn77.k4urcfbm.ni8dbmo4.stjgntxs.sbcfpzgs div.sej5wr8e div.rq0escxv.l9j0dhe7.du4w35lb.j83agx80.pfnyh3mw.i1fnvgqd.gs1a9yip.owycx6da.btwxx1t3.hv4rvrfc.dati1w0a.discj3wi.b5q2rw42.lq239pai.mysgfdmx.hddg9phg:nth-child(2) > div.rq0escxv.l9j0dhe7.du4w35lb.j83agx80.cbu4d94t.d2edcug0.hpfvmrgz.rj1gh0hx.buofh1pr.g5gj957u.p8fzw8mz.pcp91wgn.iuny7tx3.ipjc6fyt")))
print("Dub")
except:
print("Failed")
driver.quit()
The Email Address field within the webpage contains dynamic elements.
To locate the Email Address field instead of presence_of_element_located() you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using XPATH and the text #:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., '#')]"))).text)
Using XPATH and the texts # & com:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., '#') and contains(., 'com')]"))).text)
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
I'm trying to select the first value of a list (北京 in this case) from the website: https://search.jd.com/Search?keyword=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&qrst=1&wq=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&stock=1&page=5&s=121&click=1
The xpath is:
//*[#id="ttbar-mycity"]/div[2]/div[2]/div/div/div[1]/a
My code is:
browser.find_element_by_xpath("//ul[#id='ttbar-mycity']/div[2]/div[2]/div/div/div[1]/a[.= '北京']").click()
But it throws a "No such element exception"
this is the html:
UPDATE:
this is the full code so far (thanks DebanjanB):
browser.get("https://global.jd.com/")
browser.find_element_by_id("key").send_keys(mysearch)
browser.find_element_by_id("search-btn").click()
wait.until(EC.title_contains(mysearch))
ActionChains(browser).move_to_element(WebDriverWait(browser, 50).until(EC.visibility_of_element_located((By.XPATH, "//span[#class='ui-areamini-text' and #title='北京']")))).perform()
WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='dd dorpdown-layer']//div[#class='ui-areamini-content-list clearfix']/div/a"))).click()
It throws a TimeoutException at the line
ActionChains(browser).move_to_element(WebDriverWait(browser, 50).until(EC.visibility_of_element_located((By.XPATH, "//span[#class='ui-areamini-text' and #title='北京']")))).perform()
To click on the first value within the list 北京 you need to:
Mouse Hover the element with text as 北京 inducing WebDriverWait for visibility_of_element_located()
Then click on the first element inducing WebDriverWait for element_to_be_clickable()
You can use the following Locator Strategies:
browser.get('https://search.jd.com/Search?keyword=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&qrst=1&wq=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&stock=1&page=5&s=121&click=1')
ActionChains(browser).move_to_element(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[#class='ui-areamini-text' and #title='北京']")))).perform()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='dd dorpdown-layer']//div[#class='ui-areamini-content-list clearfix']/div/a"))).click()
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 detailed discussion on NoSuchElementException in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
I am making a script that will navigate through a website and perform some actions for me. But I am stuck trying to make my script click on the following elemnt:
<img src="../Images/copy.gif">
As there is no ID or class name for this element, i tried to find the element by xpath and clicking on it by using the following code:
import selenium
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
from selenium.webdriver.common.keys import Keys
import time
....
copy = driver.find_element_by_xpath('//[#id="divGrid"]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/span/a[2]')
copy.click()
This isn't working, so I am open to suggestions to how to solve this issue.
The desired element is a JavaScript enabled element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Copy Trip']>img[src$='/Images/copy.gif']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Copy Trip' and contains(#href, 'OnCopy')]/img[contains(#src, '/Images/copy.gif')]"))).click()
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
the html I'm digging (there is no iframe on it) is:
<div id="app" class="container-fluid">
I try to get the block of the DOM tree corresponding to the above div
this is my code:
from selenium.common.exceptions import NoSuchElementException
try:
app = driver.find_element_by_xpath('//div[#id="app"]')
print(4444444444444444, app)
is_vue_app_loaded = len(app.text) != 0
except NoSuchElementException as e:
print(f'e: {e}')
I get this error:
4444444444444444 <selenium.webdriver.firefox.webelement.FirefoxWebElement
(session="c8da2a89-63fd-4dce-bdb5-28fdf0e67b01",
element="b9be2828-115e-4965-8ebc-b75c09236193")>
e: Message: Unable to locate element: //div[#id='app']
Is there a way to ask the driver to return me the raw html so I can check that the DOM try I think I have is the one I have actualy ?
Check if the element is present inside any iframe.If there you need to switch it first.
If Not then try.
Induce WebDriverWait() and visibility_of_element_located()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[#id='app' and #class='container-fluid']")))
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
If your usecase is to return me the raw html of the element you have to induce WebDriverWait for the visibility_of_element_located() inconjunction with get_attribute("outerHTML") and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.container-fluid#app"))).get_attribute("outerHTML"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='container-fluid' and #id='app']"))).get_attribute("outerHTML"))
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
http://architects-register.org.uk/list/regions
So i have this website which i want to be able to have the code click the button A, then B and C and so on. How should i do that. I tried this code which tries to get the value of the child of the element then click the buttons accordingly but it does not work. I suspect there's some other issues aside the error below.
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
url = 'http://architects-register.org.uk/'
driver.get(url)
find_uk = driver.find_element_by_id('ctl00_hlCountiesT').click()
elements = alphabets[0].find_element_by_tag_name('li')
def getChild(el):
return el.children[0].children[0]
for element in elements:
element = element[elements]
(getChild(element)).click()
NameError: name 'alphabets' is not defined
alphabets isn't being imported or defined anywhere in the given code block, so trying to access an index on it will fail in the way shown. It looks like alphabets should probably be the result of calling find_elements_by_tag_name or find_elements_by_class_name which would return a list of your buttons which you could then iterate over and click on.
Induce WebDriverWait and visibility_of_all_elements_located() and then click on each link.
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.Firefox()
url = 'http://architects-register.org.uk/'
driver.get(url)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"ctl00_hlCountiesT"))).click()
elements =WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[#class='alphabet_list_content']//li//input")))
for ele in range(len(elements)):
elements = WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#class='alphabet_list_content']//li//input")))
elements[ele].click()
To click on the button A, then B and C and so on you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("http://architects-register.org.uk/list/regions")
for i in range(1, len(WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.alphabet_list_content>ul li"))))):
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.alphabet_list_content>ul li:nth-of-type({})>a>input".format(i)))).click()
driver.quit()
Using XPATH:
driver.get("http://architects-register.org.uk/list/regions")
for i in range(1, len(WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#class='alphabet_list_content']/ul//li"))))):
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='alphabet_list_content']/ul//following-sibling::li[{}]/a/input".format(i)))).click()
driver.quit()
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