1

I'm a newbie to Selenium/Python. Got stuck while trying to copy data. I'm trying to copy a data into a list. Each time I try to highlight the element name in XPath/CSS selectors, it always gives me a error saying "element not found". Could somebody help me out in finding the exact CSS selector/xpath for the same? Below is the code which I have tried for CSS selectors

browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load page
links =[]

link = browser.find_element_by_css_selector("h2 > a")

#for link in links:
link.send_keys(Keys.CONTROL + Keys.RETURN)
link.send_keys(Keys.CONTROL + Keys.PAGE_UP)

elem = browser.find_element_by_css_selector(".content.clearfix>div>fieldset>div>ul>li>span")
elem.send_keys("bar")
elem.send_keys(Keys.CONTROL, 'a') #highlight all in box
c=elem.send_keys(Keys.CONTROL, 'c') #copy
#elem.send_keys(Keys.CONTROL, 'v') #paste
print c
Mosam Mehta
  • 1,658
  • 6
  • 25
  • 34
Python Learner
  • 129
  • 1
  • 11

3 Answers3

1

exact css selector for the element:

elem = browser.find_element_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span")

You can learn css selector from here.

Anuj Kumar
  • 163
  • 10
  • Thanks it helped. I'm trying to copy the text from the css particular id. It always gives me a error when i'm tring to copy. `elem = browser.find_element_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span").text` Error is 'list' object has no attribute 'text'... Can you like help me figure out what the error is. – Python Learner Oct 01 '15 at 06:02
  • first store all elements in list elems & then iterate over it. use find_elements_by_css_selector(..) inspite of find_element_by_css_selector(..) `elems = browser.find_elements_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span") for elem in elems: print elem.text` – Anuj Kumar Oct 01 '15 at 06:20
  • No error as well as no output. I mean when i execute the code below the interpreter is just empty. Also i tried to print elems without for loop. It just shows a empty list. – Python Learner Oct 01 '15 at 06:53
1

Answer for your second query:

from selenium import webdriver

browser = webdriver.Firefox()

browser.implicitly_wait(3)

browser.get("http://ae.bizdirlib.com/taxonomy/term/1493")

links = browser.find_elements_by_css_selector("h2 > a")
links[0].click()

elems = browser.find_elements_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span")

for elem in elems:
    print elem.text

OUTPUT:

=======

Sabbro - F.Z.C
Ajman
United Arab Emirates
Free Zone(Ajman Free Zone)
Click Here to Buy United Arab Emirates Full Data
Business Directory Database Supermarket
Anuj Kumar
  • 163
  • 10
  • You my boy are a genius !!! Thanks a ton for your help. If you have a idea on how to send these values to excel/a framework where i can store them it would be great too ... Thanks for the help. Been stuck since 3 days with this issue. – Python Learner Oct 01 '15 at 08:02
  • Facing this issue while trying to integrate the whole code. So i created a function called capture. which will call the code above to fetch the necessary information above asa described by you but the problem is the function is not calling. Is it because of the for loop? Can you help me out. I'm pasting the code below. – Python Learner Oct 01 '15 at 08:34
  • `browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load pagelinks = browser.find_elements_by_css_selector("h2 > a")for link in links:link.send_keys(Keys.CONTROL + Keys.RETURN)link.send_keys(Keys.CONTROL + Keys.PAGE_UP)time.sleep(5)test() #### want to call thislink.send_keys(Keys.CONTROL + 'w')def test():elems = browser.find_elements_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span")for elem in elems:print elem.text` – Python Learner Oct 01 '15 at 08:34
  • Can you help me with this [Link](http://stackoverflow.com/questions/32883257/calling-a-function-inside-for-loop-throws-a-nameerror) in SO ... Thanks a ton in advance for the help. – Python Learner Oct 01 '15 at 12:49
  • If you have another question, please post a new question. – JeffC Oct 05 '15 at 23:59
0

Keeping in mind you've been given the css-selector already, a thing you're missing is the Selenium waits for the elements to be displayed and interactive. Every time the web page's state change (AJAX, reload etc.) you need to have a way of waiting for the elements you need.

Note that the Explicit Waits are the recommended solution. And you really shouldn't mess both waits (Explicit and Implicit).

A simple example:

from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myElementId"))
    )
ekostadinov
  • 6,880
  • 3
  • 29
  • 47
  • I have added a explicit wait time.sleep(5). Yes i am abble to find the element now. But unable to find the text of the particular element. – Python Learner Oct 01 '15 at 06:50
  • Using `time.sleep(5)` is a very bad practice. Can't you adopt the Explicit wait from the example? – ekostadinov Oct 01 '15 at 07:00