I am attempting to write a for loop to select product UPC's on a website. Essentially I have a list of UPC's in an Excel file that I want to loop through to EITHER: (1) Find the item through the search bar and select it OR (2) Item is not found and I want to add it to a list of Not Found UPCs. This is where I am at so far. I am unsure if I should use an IF ELSE statement or something else. I would really appreciate any thoughts or guidance.
for upc in upc_list:
driver.find_element_by_xpath('xpath for search bar').clear()
driver.find_element_by_xpath('xpath for search bar').send_keys(upc)
WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.XPATH, '//*[#id="'+upc+'"]'))).click()
if the element is not found an exception will be thrown and you have to catch it with a try except
upc_list=[]
not_found_list=[]
for upc in upc_list:
driver.find_element_by_xpath('xpath for search bar').clear()
driver.find_element_by_xpath('xpath for search bar').send_keys(upc)
try:
WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.XPATH, '//*[#id="'+upc+'"]'))).click()
except:
print('no results for '+upc)
not_found_list.append(upc)
continue
print('results for '+upc)
# utc is found
# do what ever you want with it
Related
I'm trying to find a specific element on a page.
The page will automatically change the page content after the page is loaded and after certain validations.
So, after the page is loaded, we try to determine whether the page has been loaded or not based on whether there is a specific element in the changed page content.
but it doesn't work
I want to do the following code is executed If the element is found within the specified time.
If I can't find it by that time, Close Selenium Object and the script.
#Waiting for detect Element
try:
UserListElement = WebDriverWait(browser, 5).until(
EC.presence_of_element_located((By.ID, "mainUserSearchDiv"))
)
except TimeoutException: #Can't find element during time.
print("Cannot find element")
browser.close #Close selenium.
finally: #Can find Element
print("Complite Load Page")
Looks like your problem is with browser.close.
Instead of close it should be close().
So please change from
browser.close
to
browser.close()
And your code should be correct
Im trying to perform select and click action from the search box result dropdown for testing purpose. Though i dont get ant error but i'm stuck and not able to do so, search results came then disappeared immediately. Please any one help me out. Im using Python script to automate webdriver. Here is the screenshot below for reference.
. I have tried webdriverwait for same action but it gives Timeout exception error. If there is any child actions from CSS to perform let me know. Here is what i tried
search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")
dropdown = WebDriverWait(driver, 4).until(
EC.presence_of_element_located((By.XPATH, "//li[text()='flowers']")))
Apart from this snippet, i want to rather just perform enter key operation, where i get query result for 'flower' on this ecomm. website.
Here is the website URL- https://paytmmall.com
Once you type flower in the input field, there are multiple options appearing based on the input provided. They are in li tags and under b tag.
Code :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://paytmmall.com/")
search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
wait.until(EC.visibility_of_element_located((By.XPATH, "(//li)[4]/descendant::b[contains(text(),'flowers')]"))).click()
time.sleep is just for visibility purpose. you can remove that as well.
Also this xpath (//li)[4]/descendant::b[contains(text(),'flowers')] is based on xpath indexing , since I think you wanna select the 4th option which is flower itself. In case you wanna select a different option, you would have to write the different xpath.
In case you are looking to just select the searched item, it's better to pass enter key once you type flower in the input field.
You can use the below code for that :
search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
search.send_keys(Keys.RETURN)
The suggested options are not containing the text directly in li elements, they are inside child elements inside li elements.
Try this instead:
search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")
dropdown = WebDriverWait(driver, 4).until(
EC.visibility_of_element_located((By.XPATH, "//li//*[text()='flowers']")))
My code:
website = browser.find_element_by_link_text('Website')
if website:
website.click()
else:
print('no website')
What I am trying to do is click the button if it is available on the page. If the button isn't available I want it to print no website available on the console and proceed to the next step.
I do not know what I am doing wrong does anyone know how to do fix this?
Thanks in advance I am new to coding!
Before you find an element, you need first visit a website.
driver = webdriver.Firefox()
print('Acessando web site: {}'.format(os.getenv('VISIT_URL')))
driver.get('www.example.com')
#here onwards you can access browser elements like buttons links etc
You are giving a instance of Web element to if() condition, where a boolean expression is expected.
1) Try checking the presence first using find_elements_by_link_text() # _elements_
if len(driver.find_elements_by_link_text('Website')) > 0:
driver.find_element_by_id('Website').click()
2) Or use expected_conditions to check whether the element is available; expected_conditions documentation
3) Or use try/except block;
try:
website = driver.find_elements_by_link_text('Website')
except NoSuchElementException:
# code to execute if the expected element is not available
I am fairly new to Selenium, I am trying to find a Contact info element in a page, and click it if it exists. Many times, what happens is, the element is in all caps such as CONTACT, sometimes Contact and sometimes contact. So I've stored these cases in a variable and I'm using find_element_by_partial_link_text to find the right element and click on it. I'm using exception handling (try and except) and if loop to check each condition. This is my code:
from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
browser = webdriver.Chrome()
browser.implicitly_wait(30)
browser.maximize_window()
ab = 'Contact'
bc = 'CONTACT'
cd = 'contact'
browser.get('https://www.dominos.co.in/store-location/pune')
try:
if browser.find_element_by_partial_link_text(ab).is_displayed():
browser.find_element_by_partial_link_text(ab).click()
elif browser.find_element_by_partial_link_text(bc).is_displayed():
browser.find_element_by_partial_link_text(bc).click()
elif browser.find_element_by_partial_link_text(cd).is_displayed():
browser.find_element_by_partial_link_text(cd).click()
except NoSuchElementException:
print("No such element found")
browser.close()
So if Contact element is present in any webpage, this code is able to click on it, but if other two elements are present, it goes directly into except and prints No such element found. If you guys could help me tackle this scenario, I would really appreciate it :)
Use xpath with translate() to ignore the case of the text in the html. You can also use find_elements to avoid the try except:
elements = browser.find_elements_by_xpath('//a[translate(text(),"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz") = "contact"]')
if elements and elements[0].is_displayed():
elements[0].click()
Try using translate() functions in xpath ,these functions helps you to deal with case sensitivity.
Hope this helps.
I am trying to iterate through a list, this code block is part of a for loop - what I'm trying to accomplish is that if an element is not present go to the next item in the list. I'm testing it with two pages in a list, the first iteration does contain the class 'buggybox' however the second when then for loop gets to the second itteration len(warning) is still being printed even and the loop doesn't get to the else statement. I suspect it's an issue with my python code rather than it being a selenium issue. Either way I would be grateful for some help.
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'windowAutomatch')))
run_automatch_button = driver.find_element_by_id('btnRunAutomatch')
run_automatch_button.click()
warning = driver.find_elements_by_class_name('buggybox')
if len(warning):
print(len(warning))
continue
else:
#Saves suggested matches
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'windowConfirmAutomatch')))
save_suggested_matches_button = driver.find_element_by_link_text('Save Suggested Matches')
print('found')
driver.execute_script("arguments[0].click();", save_suggested_matches_button)
WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.ID, 'windowConfirmAutomatch')))
print("AutoMatch Complete for " + fund)