Unable to find element using Selenium - python

Using Selenium, I am unable to locate the "email" element on the Udemy website.
Here's what I tried:
browser.get('https://www.udemy.com/join/login-popup/')
browser.implicitly_wait(5)
email = browser.find_element(By.ID, 'email--1')
print(email)
but it gives NoSuchElementException while "email" element isn't even in an iframe, as far as I know.
So, how can I locate this specific element?

In this case, you're probably looking for the element before the page loads.
You should use the WebDriverWait class of Selenium and the condition presence_of_element_located of the expected_conditions, as shown in the example below:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
browser.get('https://www.udemy.com/join/login-popup/')
timeout = 20 # Number of seconds before timing out.
email = WebDriverWait(browser, timeout).until(EC.presence_of_element_located((By.ID, 'email--1')))
email.send_keys("example#email.com")
In the above snippet, Selenium WebDriver waits up to 20 seconds before throwing a TimeoutException, unless it finds the element to return within the above time. WebDriverWait, by default, calls the ExpectedCondition every 500 milliseconds until it returns successfully.
Finally, presence_of_element_located is an expectation for determining whether an element is present on a page's DOM, without necessarily implying that the element is visible. When the element is found, the locator returns the WebElement.

Related

locate an a element and click on it in selenium pyhton

I have this page and I want to click on the a element that sends an email(the one that is highlighted in the screenshot below
and I have tried to find this element using By.XPATH
email = driver.find_element(By.XPATH, "//a[contains(#class, 'email-old-32')]").click()
and By.CLASS_NAME
email = driver.find_element(By.CLASS_NAME, 'email-old-32').click()
and in both situations i'm getting an error no such element, does anyone know what am I doing wrong?
There are several possible thing that may cause this:
You need to wait for element to be loaded and to become clickable. WebDriverWait expected_conditions can be used for that.
If this is your case instead of email = driver.find_element(By.XPATH, "//a[contains(#class, 'email-old-32')]").click() try this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#class, 'email-old-32')]"))).click()
The element class attribute value can be dynamic. Make sure it is not changing. In case it does - you need to find another, stable locator for that element.
The element can be inside iframe. In this case you will need to switch into the iframe.
Possibly you opened a new tab where the goal element is presented but forget to switch to a new tab.

Why can't find element using Selenium?

I'm trying to find an element in Yandex.ru through selenium and click on it.enter image description here
the code is being processed but the click is not happening, I'm assuming selenium doesn't see the element.
def start_bot():
for i in req:
browser.get('https://yandex.ru/search/?lr=65&text='+i)
time.sleep(2)
print(browser.find_element(By.XPATH, '//*[#id="search-result"]/li[1]/div/div[2]/div[2]').click()
start_bot()
Referring to the parent class is not an option, how can I solve this problem?
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
# Assuming browser is already defined
try:
max_delay = 10 # seconds
browser.get('https://yandex.ru/search/?lr=65&text=1')
button = WebDriverWait(browser, max_delay).until(
EC.element_to_be_clickable((By.XPATH, '//*[#id="search-result"]/li[1]/div/div[2]/div[2]'))
)
button.click()
except TimeoutException:
print('Timeout: Element not found or clickable')
Instead of waiting for a fixed delay (2 seconds) as you have done, this code will wait for the element specified by the XPath to be clickable. If the element is not clickable after max_delay (10 seconds), an error of type TimeoutException will be thrown.
You can easily adapt this code to open multiple urls in a loop (as you have done in your code).
PS: Please fix your code's formatting (the function call start_bot() is rendered as text, not code.)

Button element not interactable Selenium

I'm trying to write a script to automate some tasks with Selenium and Python, and every time I try to click on a button
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
driver = webdriver.Chrome(options=options)
xpath = "/html/body/app-root/app-prime/div/mat-sidenav-container//app-detail-component/main//div/span/button[#aria-label='Prenota']"
# Wait for the element to be visible, always true
WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located((By.XPATH, xpath)))
# Try to click on element, get an error
driver.find_element(By.XPATH, xpath).click()
I get the following error
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I know for sure that the element gets located correctly and has to match, but it should act only on the first one.
I tried:
Trying to click child tags, such as others divs and span
Waiting for it to be clickable
Waiting with an implicit wait
None of those activities were successful
Edit: Apparently the issue olly exist on my machine
It could be not clickable for a number of reasons. You might want to check if there is some element on the page layered on top so that that element is not interactable at that time, e.g some popup/iframe etc. There could be some other element that will receive click at that time.
You could try an actions click - something like this
myElement = driver.find_element_by_xpath("myXpath")
webdriver.ActionChains(driver).move_to_element(myElement).click(myElement).perform()
One of this should work.
IJavaScriptExecutor executor = (IJavaScriptExecutor)WebDriver.Driver;
executor.ExecuteScript("arguments[0].click();", webElement);
Actions actions = new Actions(WebDriver.Driver);
actions.MoveToElement(webElement).Click().Perform();
Note - these are c sharp code. try to do the same in java.

locating hide element using selenium and python

I have an element that after clicking the button it builds a div with Ajax and I can't get the element.
Click button:
Show div:
But in my code doesn't work
vv = drive.find_element_by_xpath('//*[#id="vUPDATE_0001"]')
Error Message:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="vUPDATE_0001"]"}
I think what you're looking for is to wait until the div is visible. Luckily, selenium has just that!
You can read more over here.
By using WebDriverWait along with expected_conditions, your example code would look like this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
timeout = 10 # Wait 10 seconds. If it doesn't appear in 10 seconds then throw an error
vv = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, '//*[#id="vUPDATE_0001"]')))
For many different circumstances, there are different conditions, such as wait for an element to be clickable, or wait an alert to pop up. We are using presence of element located, which is the same as find_element_by_[condition]. Then we are setting the condition with By.XPATH. There are many different conditions which represent their find_element_by_ alternative, such as By.ID, By.CLASS_NAME, and By.NAME.

Trying to use selenium to automate signups. Running into a problem

Currently trying to automate signups on 'mail.com' using Selenium. So far i've managed to get the program to go to the URL. The problem i'm having is that even when I copied the full XPATH of "Sign Up" i'm getting an:
"selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[114]/td[2]"}"
error
Here is the code i'm working with so far:
import selenium
import time
from selenium.webdriver.common.by import By
driver = selenium.webdriver.Chrome(executable_path='pathtochromedriver')
driver.get('https://www.mail.com/')
driver.maximize_window()
# Delay added to allow elements to load on webpage
time.sleep(30)
# Find the signup element
sign_up = driver.find_element_by_xpath('/html/body/table/tbody/tr[114]/td[2]')
Try using ActionsChains to scroll to ensure the element is in view.
from selenium.webdriver.common.action_chains import ActionChains
some_page_item = driver.find_element_by_class_name('some_class')
ActionsChains(driver).move_to_element(some_page_item).click(some_page_item).perform()
Also another tip... instead of simply using time.sleep() to wait for an element to appear, instead use WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait_for_item = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME ,"some_class_name")))
30 is the amount of seconds that it will wait until the item appears; however if it appears before 30 seconds then it will immediately continue execution. If 30 seconds passes and the item doesn't appear a timeout error will occur.

Categories

Resources