I am trying to make an auto-checkout script on https://www.footish.se/sneakers/fila-wmns-disruptor-run-1010866-60m
I have made it to the checkout page, but unable to enter my email in the "email" input-field.
The code looks like this
email = driver.find_element_by_xpath("/html/body/div/span/div/div/div/div[1]/div/div/div[1]/div/form/div[2]/div[1]/div/label/div/div/input")
email.send_keys("test#email.com")
Have implemented some sort of function to wait for the desired elements untill they are loaded in. One example->
while not find:
try:
find = driver.find_element_by_xpath("/html/body/form/div[5]/div/div[4]/div[1]/div[1]/div[2]/div[1]/div[10]/div[1]/h2")
print("Loaded info")
except:
continue
The error i am getting is this.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/span/div/div/div/div[1]/div/div/div[1]/div/form/div[2]/div[1]/div/label/div/div/input"}
How would i resolve this? Thanks in advance....
It seems like the email field is a dynamicallly generated element and so when a check is initially made it is not yet present. You may try and use the until method to wait for a specific time and see if it indeed does get applied to the DOM.
Sample code
driver = webdriver.Chrome()
driver.get("url")
try:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "myDynamicElement")) # set your id here
)
finally:
driver.quit()
Do update if it doesn't work. It's then likely to do with frame or wrong path issues.
Related
I am having trouble with finding the compose button in Gmail. Every time I run it I get the following error message saying that the element doesn't exist
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#class="T-I T-I-KE L3"]"}
I have tried multiple things including:
ComposeButton = driver.find_element(By.XPATH, '//*[#jscontroller="eIu7Db"]')
ComposeButton = driver.find_element(By.XPATH, '//*[#class="T-I T-I-KE L3"]')
ComposeButton = driver.find_element(By.XPATH, '//*[#jsaction="click:dlrqf; clickmod:dlrqf"]')
They have all returned with the same error message
How do I access the compose button?
The greyed out part of the HTML in the linked image is for the compose button
Try this:
find_element(By.XPATH, "//div[#class='aic']/div/div")
This Worked for me
driver.find_element(By.CSS_SELECTOR, "[gh='cm']").click()
(N.B = change the word driver as per your code)
I want to locate the elements of a popup on some page,
the popup html is written in an iframe,
also the popup is triggered by clicking a link on the main page.
The weird thing is, if I run the whole code, I can not locate the 'target' element:
dr = webdriver.Chrome('chromedriver.exe', options=chrome_options)
modify = (By.CLASS_NAME, "modify")
ec_visible(dr, modify).click()
popup = (By.CLASS_NAME, "add-addr-iframe")
dr.switch_to.frame(ec_visible(dr, popup))
target = (By.CLASS_NAME, "cndzk-entrance-division-header-click")
ec_visible(dr, target).click()
def ec_visible(driver, locator):
return WebDriverWait(driver, 5).until(EC.visibility_of_element_located(locator))
But, if I first open the popup then locate, it works.
First:
modify = (By.CLASS_NAME, "modify")
ec_visible(dr, modify).click()
#popup = (By.CLASS_NAME, "add-addr-iframe")
#dr.switch_to.frame(ec_visible(dr, popup))
#target = (By.CLASS_NAME, "cndzk-entrance-division-header-click")
#ec_visible(dr, target).click()
Then: (works too if I manually open the popup and run this code)
#modify = (By.CLASS_NAME, "modify")
#ec_visible(dr, modify).click()
popup = (By.CLASS_NAME, "add-addr-iframe")
dr.switch_to.frame(ec_visible(dr, popup))
target = (By.CLASS_NAME, "cndzk-entrance-division-header-click")
ec_visible(dr, target).click()
Appreciate if you can point out my problem!
Here is the exception from shell:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
This is the html screenshot,
sometimes the content in iframe can not even be seen.
iframe not extendable
when iframe extendable
7/26 update
I am wondering whether I asked the right question which may lead you guys just focusing on my code part. Since my code works(seperately), the elements and frames approach are good.
I step back and find one detail which may help but I don't know how it matters.
Here are the two shots about ctrl-F some element in the page source:
Normal result: target found and highlighted
Weird result: target found and no highlight
I mean when the page exists 'weird result', my code does not work.
PS. The page is the order-confirmation part of an e-commerical site, but the site groups its goods into two types which led to TWO types order page.
For ifarmes you have frame_to_be_available_and_switch_to_it as the EC.
So try this:
WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "add-addr-iframe")))
target = (By.CLASS_NAME, "span.cndzk-entrance-division-header-click")
ec_visible(dr, target).click()
The following script follows a page in Instagram:
browser = webdriver.Chrome('./chromedriver')
# GO INSTAGRAM PAGE FOR LOGIN
browser.get('https://www.instagram.com/accounts/login/?hl=it')
sleep(2)
# ID AND PASSWORD
elem = browser.find_element_by_name("username").send_keys('test')
elem = browser.find_element_by_name("password").send_keys('passw')
# CLICK BUTTON AND OPEN INSTAGRAM
sleep(5)
good_elem = browser.find_element_by_xpath('//*[#id="react-root"]/section/main/div/article/div/div[1]/div/form/span/button').click()
sleep(5)
browser.get("https://www.instagram.com")
# GO TO PAGE FOR FOLLOW
browser.get("https://www.instagram.com/iam.ai4/")
sleep(28)
segui = browser.find_element_by_class_name('BY3EC').click()
If an element with class BY3EC isn't found I want the script to keep working.
When an element is not found it throws NoSuchElementException, so you can use try/except to avoid that, for example:
from selenium.common.exceptions import NoSuchElementException
try:
segui = browser.find_element_by_class_name('BY3EC').click()
except NoSuchElementException:
print('Element BY3EC not found') # or do something else here
You can take a look at selenium exceptions to get an idea of what each one of them is for.
surround it with try catches, than you can build a happy path and handle failures as well, so your test case will always work
Best practice is to not use Exceptions to control flow. Exceptions should be exceptional... rare and unexpected. The simple way to do this is to get a collection using the locator and then see if the collection is empty. If it is, you know the element doesn't exist.
In the example below we search the page for the element you wanted and check to see that the collection contains an element, if it does... click it.
segui = browser.find_elements_by_class_name('BY3EC')
if segui:
segui[0].click()
I am trying to write a script to delete all my comments on my profile in Reddit.
So I am currently using Selenium to log-in and try to delete my comments, however I am stuck at the point when after my script press delete on my comment and it changes to "Are you sure Yes/No" then it can't find the "Yes" element by Xpath. The following code throws the error:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message:
Element is not currently visible and so may not be interacted with
Stacktrace:
My code is as follows:
del_button = driver.find_element_by_xpath("//*[contains(#id,'thing_"+delete_type+"')]//div[2]/ul/li[7]/form/span[1]/a")
del_button.click()
time.sleep(3)
yes_button = driver.find_element_by_xpath("//*[contains(#id,'thing_"+delete_type+"')]//div[2]/ul/li[7]/form/span[1]//a[1]")
yes_button.click()
time.sleep(3)
As there could be several hidden elements with same attributes on page, you might need to use index to click on exact element:
driver.find_elements_by_xpath('//a[#class="yes"]')[N].click() # N is the index of target link
I f you can't define exact index, you can use below code:
from selenium.common.exceptions import ElementNotVisibleException
for link in driver.find_elements_by_xpath('//a[#class="yes"]'):
try:
link.click()
break
except ElementNotVisibleException:
pass
https://gist.github.com/codyc4321/724f05aca8f6775e2fc1
Hi, bitbucket changed their login page, and is giving me a hassle. Based on the following gist, using driver.click_button causes:
ElementNotVisibleException at /bitbucket/create-repo
Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpSNzLIl/extensions/fxdriver#googlecode.com/components/command-processor.js:9981)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpSNzLIl/extensions/fxdriver#googlecode.com/components/command-processor.js:12517)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpSNzLIl/extensions/fxdriver#googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpSNzLIl/extensions/fxdriver#googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpSNzLIl/extensions/fxdriver#googlecode.com/components/command-processor.js:12481)
using driver.submit_form causes error in the browser itself:
using driver.activate_hidden_element causes:
ElementNotVisibleException at /bitbucket/create-repo
Message: Element is not currently visible and so may not be interacted with
activate_hidden_element failing really took the wind outta my sails for the last 5 minutes. How can I click this stonewalling button? Thank you
Okay, so the problem is actually in your locate_element method.
When you check for the xpath: "//button[normalize-space(text())='{text}']" it finds a button successfully but it is not the login button you are looking for. If you switch that with the input xpath: "//input[#value='{text}']" it finds the right input and successfully logs you in.
You should also remove the last two lines in the BitbucketDriver.login() method because the line self.click_button(search_text="Log in") throws an AttributeError.
Your locate_element method should look like this:
def locate_element(self, search_text, xpaths=None):
if not xpaths:
xpaths = [ "//input[#value='{text}']", "//button[normalize-space(text())='{text}']",
"//a[child::span[normalize-space(text())='{text}']]", "//a[normalize-space(text())='{text}']"]
try:
return self.driver.find_element_by_id(search_text)
except:
try:
return self.driver.find_element_by_name(search_text)
except:
try:
return self.driver.find_element_by_class_name(search_text)
except:
for path in xpaths:
try:
return self.driver.find_element_by_xpath(path.format(text=search_text))
except:
pass
return None