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/[email protected]/components/command-processor.js:9981)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpHWLMyH/extensions/[email protected]/components/command-processor.js:12517)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpHWLMyH/extensions/[email protected]/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpHWLMyH/extensions/[email protected]/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpHWLMyH/extensions/[email protected]/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?
Either move to element and then click:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(browser)
actions.move_to_element(option_in_combobox).click().perform()
or, scroll into it's view:
browser.execute_script("arguments[0].scrollIntoView();", option_in_combobox)
or, click the element via javascript:
browser.execute_script("arguments[0].click();", option_in_combobox)
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.
There is an existing problem in selenium 2.49 that may cause this TypeError
. You would need to downgrade to 2.48:
pip install selenium==2.48
Like mentioned you should first move to the option element before clicking on it: actions.move_to_element(option_in_combobox).click().perform()
. Now is the 'button_choose' element not visible because the browser scrolled down. To fix this you need scroll to the top and then click on the button:
browser.execute_script("window.scrollTo(0, 0);") # scroll to top
button_choose = browser.find_element_by_xpath(CHOOSE_BUTTON_XPATH)
button_choose.click()
try this before selecting the "Not Currently invisible Element", and after clicking on the Dropdown menu:
browser.execute_script("document.getElementsByClassName('dropdown-menu inner selectpicker')[0].setAttribute('style', '');")
I tried in java and its working very fine. I request to please look on for loop mainly as here i am able to select options one by one. I just used Thread.sleep, as i know we can use waits also. To make similar to question i just appended to StringBuffer here, i know we can added to any collectors in java. I am not followed exactly as accepting age etc. as i said i concentrated on that drop down selection..
public class Dog {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@BeforeClass()
public void setUp() throws Exception {
// driver = new FirefoxDriver();
System.setProperty("webdriver.chrome.driver", "E:\\selenium_setups\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
baseUrl = "http://example.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testStackoverflowIssue() throws Exception {
driver.get(baseUrl);
driver.findElement(By.cssSelector(".btn.btn-success")).click();
driver.get("http://example.com/ogloszenia-kobiet.html");
List<WebElement> options=driver.findElements(By.xpath(".//*[@id='select_city']/li/form/div/div/ul/li"));
for(int i=1; i<=options.size(); i++){
driver.findElement(By.xpath("(//button[@type='button'])[4]")).click();
driver.findElement(By.xpath("//ul[@id='select_city']/li/form/div/div/ul/li["+i+"]")).click();
driver.findElement(By.cssSelector("button.btn.btn-success")).click();
Thread.sleep(5000);
verificationErrors.append(driver.getCurrentUrl());
System.out.println(driver.getCurrentUrl() +" >>> iteration " +i);
driver.navigate().to("http://example.com/ogloszenia-kobiet.html");
Thread.sleep(5000);
}
}
}
I hope this will helps you in python to select drop down values..
output
Thank You, Murali
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With