Considering the HTML:
I want to select the paragraphs to the left with Selenium. Tried my hand at class_name and id but got NoSuchElementException. Why am I getting this error? I mean the elements are clearly there then why isn't Selenium recognizing those?
Methods I tried:
element = driver.find_element_by_xpath("//div[#id = 'mar-2019']//div[#class='report_data']").text
element = driver.find_element_by_id("mar-2019").text
element = driver.find_element_by_class_name("report_data").text
Where am I going wrong?
To handle dynamic element induce WebDriverWait() and wait for visibility_of_element_located()
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"mar-2019"))).text
OR
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"div#mar-2019"))).text
You need to import following libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
There are multiple child <p> elements within multiple parent/ancestor <div> elements. To extract the contents of the <p> elements within the parent <div id="mar-2019"> element you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and get_attribute("innerHTML"):
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".data-row")))])
Using XPATH and text attribute:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#id='mar-2019']//div[#class='report_data']//p")))])
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
Reference
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
Related
I am trying to get the following count of an Instagram account. I believe that the XPATH is correct an exists. Here's a screenshot showing it exists when I search for it:
This is my code:
wait = WebDriverWait(driver, 30)
followers = wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[1]/section/main/div/ul/li[2]/button/div/span")))
print(followers.get_attribute("title"))
I have even looked at similar projects that find the following count and our code is almost exactly the same.
The desired element is a dynamic element, so to locate the element 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 strategy:
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(., 'followers')]//span[#class and #title and text()]"))).get_attribute("title"))
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 want to use selenium to login in the website https://www.winamax.es/account/login.php?redir=/apuestas-deportivas. The case is that I don't find the xpath/id/text to get te next code running successfully:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.ChromeOptions()
options.add_argument("window-size=1920,1080")
options.add_argument('--disable-blink-features=AutomationControlled')
driver=webdriver.Chrome(options=options,executable_path=r"chromedriver.exe")
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver=driver, timeout=15).until(
lambda x: x.execute_script("return document.readyState === 'complete'")
)
upload_field = driver.find_element_by_xpath("//input[#type='email']")
I don't only want the specific xpath for this example, but I prefer a method to obtain the xpath or something similar to get the code working for other parts of the website
<iframe id="iframe-login" data-node="iframe" name="login" scrolling="auto" frameborder="1" style="min-height: 280px; width: 100%;"></iframe>
Your element is in an iframe. Switch to it.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe-login")))
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Full working code.
wait = WebDriverWait(driver, 10)
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe-login")))
upload_field = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#type='email']")))
upload_field.send_keys("stuff")
The email element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iframe-login")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='email']"))).send_keys("scraper#stackoverflow.com")
Using XPATH:
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='iframe-login']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#type='email']"))).send_keys("scraper#stackoverflow.com")
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 couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
I am using selenium with python I want to insert the username and email inputs containing in frame
Here is the link
I am trying to do this but it's not switching into the modal.
try:
time.sleep(3)
driver.switch_to.frame(driver.find_element_by_id("thanksModal"))
driver.find_element_by_xpath("//input[#type='file']").send_keys("C:\\Users\\user\\Desktop\\Resume.docx")
driver.find_element_by_id('txtName').send_keys(name)
driver.find_element_by_id('txtEmail').send_keys(email)
driver.find_element_by_id('btnSubmit').click()
time.sleep(5)
except:
pass
The elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.collabera.com/find-a-job/search-jobs/job-details/248429-python-developer-jobs-jersey-city-nj/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.apply-form")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#txtName"))).send_keys("bhupathi_turaga#stackoverflow.com")
driver.find_element_by_css_selector("input#txtEmail").send_keys("bhupathi_turaga#stackoverflow.com")
Using XPATH:
driver.get("https://www.collabera.com/find-a-job/search-jobs/job-details/248429-python-developer-jobs-jersey-city-nj/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='apply-form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='txtName']"))).send_keys("bhupathi_turaga#stackoverflow.com")
driver.find_element_by_xpath("//input[#id='txtEmail']").send_keys("bhupathi_turaga#stackoverflow.com")
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 couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
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 new to Python/Selenium (< 3 days). I am trying to click on the "grades" item of an unordered list (Please see image). I have tried find_element_by_link_text, find_element_by_css_selector but am unable to find it.
Image from inspect element
Thanks.
As per the HTML it is pretty clear that the desired field is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"frameDetail")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#grades")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"frameDetail")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#id='grades']")))
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
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python