I need to get through all the pages on my webpage. There is a dropdown box in the left upper corner on all of these pages, with all the available cities. I want to visit every page, by choosing every position in this dropdown box. The dropdown box has a scrollbar, and when I want to choose the option which is below it, it gives me exception message:
Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:9981)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:12517)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpHWLMyH/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
Heres the code :
#!/bin/env/python
# -*- coding: utf-8 -*-
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
def get_browser():
return webdriver.Firefox()
main_page_url = "http://example.com/"
basic_url = 'http://example.com/ogloszenia-kobiet.html'
def get_city_list(url) :
AGE_ACCEPT_BUTTON_XPATH = ".//*[@id='columns']/div/div[2]/section/div/div/div/div/div/div[2]/div[2]/a[2]"
COMBOBOX_XPATH = ".//*[@id='select_city']/li/form/div/button"
COMBOBOX_OPTION_XPATH = ".//*[@id='select_city']/li/form/div/div/ul/li[%s]/a/span[1]"
CHOOSE_BUTTON_XPATH = ".//*[@id='select_city']/li/form/button"
pages = []
try:
browser = get_browser()
wait = WebDriverWait(browser, 100)
browser.get(main_page_url)
time.sleep(2)
button_age_accept = browser.find_element_by_xpath(AGE_ACCEPT_BUTTON_XPATH)
button_age_accept.click()
time.sleep(10)
browser.get(url)
i = 2
while(True) :
try :
button_combobox = browser.find_element_by_xpath(COMBOBOX_XPATH)
button_combobox.click()
time.sleep(5)
element_xpath = COMBOBOX_OPTION_XPATH % i
option_in_combobox = browser.find_element_by_xpath(element_xpath)
# wait.until(EC.invisibility_of_element_located((By.XPATH, element_xpath)))
# option_in_combobox = WebDriverWait(browser, 10).until(lambda browser : browser.find_element_by_xpath(element_xpath))
option_in_combobox.click()
time.sleep(5)
button_choose = browser.find_element_by_xpath(CHOOSE_BUTTON_XPATH)
button_choose.click()
time.sleep(5)
pages.append(browser.current_url)
i += 1
except Exception, e:
print e
break
browser.close()
return pages
except Exception, e:
info = 'Generic exception\n'
print e
return []
get_city_list(basic_url)
I also tried with scroll bar, tried to move it down, but still, no effect. I can scroll only the pages that are at the top of this drop down box:
#!/bin/env/python
# -*- coding: utf-8 -*-
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
def get_browser():
return webdriver.Chrome()
main_page_url = "http://example.com/"
basic_url = 'http://example.com/ogloszenia-kobiet.html'
def get_city_list(url) :
AGE_ACCEPT_BUTTON_XPATH = ".//*[@id='columns']/div/div[2]/section/div/div/div/div/div/div[2]/div[2]/a[2]"
COMBOBOX_XPATH = ".//*[@id='select_city']/li/form/div/button"
COMBOBOX_OPTION_XPATH = ".//*[@id='select_city']/li/form/div/div/ul/li[%s]/a/span[1]"
CHOOSE_BUTTON_XPATH = ".//*[@id='select_city']/li/form/button"
pages = []
try:
browser = get_browser()
wait = WebDriverWait(browser, 100)
browser.get(main_page_url)
time.sleep(2)
button_age_accept = browser.find_element_by_xpath(AGE_ACCEPT_BUTTON_XPATH)
button_age_accept.click()
time.sleep(10)
browser.get(url)
i = 2
while(True) :
try :
button_combobox = browser.find_element_by_xpath(COMBOBOX_XPATH)
button_combobox.click()
time.sleep(5)
element_xpath = COMBOBOX_OPTION_XPATH % i
option_in_combobox = browser.find_element_by_xpath(element_xpath)
actionChains = ActionChains(browser)
scrollbar = browser.find_element_by_xpath("/html/body/section/section[2]/div/div[2]/section/div/div/div/div[1]/ul/li/form/div/div/ul")
actionChains.click_and_hold(scrollbar).perform()
actionChains.move_by_offset(0,10+i).perform()
actionChains.release()
browser.execute_script("arguments[0].scrollIntoView();", option_in_combobox)
option_in_combobox.click()
browser.execute_script("window.scrollTo(0, 0);")
button_choose = browser.find_element_by_xpath(CHOOSE_BUTTON_XPATH)
button_choose.click()
time.sleep(5)
pages.append(browser.current_url)
i += 1
except Exception, e:
print e
break
browser.close()
return pages
except Exception, e:
info = 'Generic exception\n'
print e
return []
pages = get_city_list(basic_url)
for p in pages :
with open('links.txt', 'a') as the_file:
the_file.write(p)
the_file.write('\n')
------------------------------------------------------------------------------------------------------------------------------------ UPDATE: ------------------------------------------------------------------------------------------------------------------------------------
For now on, I'm using Kubuntu 14.04
. I have Python 2.7.11
and Selenium 2.49.2
. My current code:
#!/bin/env/python
# -*- coding: utf-8 -*-
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
# def get_browser():
# options = webdriver.ChromeOptions()
# options.add_argument("--start-maximized")
# return webdriver.Chrome(chrome_options=options)
def get_browser():
return webdriver.Firefox()
main_page_url = "http://example.com/"
basic_url = 'http://example.com/ogloszenia-kobiet.html'
def get_city_list(url) :
AGE_ACCEPT_BUTTON_XPATH = ".//*[@id='columns']/div/div[2]/section/div/div/div/div/div/div[2]/div[2]/a[2]"
COMBOBOX_XPATH = ".//*[@id='select_city']/li/form/div/button"
COMBOBOX_OPTION_XPATH = ".//*[@id='select_city']/li/form/div/div/ul/li[%s]/a/span[1]"
CHOOSE_BUTTON_XPATH = ".//*[@id='select_city']/li/form/button"
pages = []
try:
browser = get_browser()
wait = WebDriverWait(browser, 100)
browser.get(main_page_url)
time.sleep(2)
button_age_accept = browser.find_element_by_xpath(AGE_ACCEPT_BUTTON_XPATH)
button_age_accept.click()
time.sleep(10)
browser.get(url)
i = 2
while(True) :
try :
button_combobox = browser.find_element_by_xpath(COMBOBOX_XPATH)
button_combobox.click()
time.sleep(5)
element_xpath = COMBOBOX_OPTION_XPATH % i
option_in_combobox = browser.find_element_by_xpath(element_xpath)
option_in_combobox.click()
button_choose = browser.find_element_by_xpath(CHOOSE_BUTTON_XPATH)
button_choose.click()
time.sleep(5)
pages.append(browser.current_url)
i += 1
except Exception, e:
print e
break
browser.close()
return pages
except Exception, e:
info = 'Generic exception\n'
print e
return []
pages = get_city_list(basic_url)
for p in pages :
with open('links.txt', 'a') as the_file:
the_file.write(p)
the_file.write('\n')
For Firefox, the code exits at element 'Gdańsk' with a message: string indices must be integers
, so it means it does not find every element in my combobox.
For Chrome and Windows XP, it exits at 'Bielsko-Biała', so again, it means it does not find every element in my combobox. ...
How can I solve this issue?