1
votes

I want to extract users from a website bu using for loop, but i don't know how can i put correctly "i" instead of number 1

after i put

user_id = browser.find_element_by_xpath("(//div[@class='_gzjax'])["+str(i)+"]").text

Traceback (most recent call last): File "D:/Code/Python/Instagram Unfollow/Instagram Unfollow.py", line 32, in user_id = browser.find_element_by_xpath("(//div[@class='_gzjax'])["+str(i)+"]").text File "C:\Python33\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) File "C:\Python33\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element 'value': value})['value'] File "C:\Python33\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute self.error_handler.check_response(response) File "C:\Python33\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"(//div[@class='gzjax'])[0]"} Stacktrace: at FirefoxDriver.prototype.findElementInternal (file:///c:/users/viktor/appdata/local/temp/tmp1r0vgw/extensions/[email protected]/components/driver-component.js:10770) at FirefoxDriver.prototype.findElement (file:///c:/users/viktor/appdata/local/temp/tmp1r0vgw/extensions/[email protected]/components/driver-component.js:10779) at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/viktor/appdata/local/temp/tmp1r0vgw/extensions/[email protected]/components/command-processor.js:12661) at DelayedCommand.prototype.executeInternal_ (file:///c:/users/viktor/appdata/local/temp/tmp1r0vgw/extensions/[email protected]/components/command-processor.js:12666) at DelayedCommand.prototype.execute/< (file:///c:/users/viktor/appdata/local/temp/tmp1r0vgw/extensions/[email protected]/components/command-processor.js:12608)

4
can't you concatenate the i var in the xpath string e.g "(//div[@class='_gzjax'])["+i+"]"user993553
@user993553 i tried like this "+i+", but it gives error i guess i;m missing somethingViktor
double check your code. looks like the underscore is getting dropped: this is in your error output: {"method":"xpath","selector":"(//div[@class='gzjax'])[0]"} so the concat is workingJim VanPetten

4 Answers

1
votes

Just concatenate i (first cast it to string) to your desired string:

for i in range (0,100):
    user_id = browser.find_element_by_xpath("(//div[@class='_gzjax'])["+str(i)+"]").text
    print(user_id)
1
votes

Try this. I think you concatenate variables into string with plus sign in python:

user_id = browser.find_element_by_xpath("(//div[@class='_gzjax'])["+i+"]").text

see how we add ("string" + variable + "string")

0
votes

You would need to explicitly convert i from int to string. How about:

for i in range (0,100):
    user_id = browser.find_element_by_xpath("(//div[@class='_gzjax'])["+str(i)+"]").text
    print(user_id)
0
votes

I don't know how many users there are on the page, but as it stands you're bound to 101.

Could just loop through the elements?

for i in browser.find_element_by_xpath("(//div[@class='_gzjax'])"):
    user_id = i.text
    print(user_id)

Something along those lines where you are iterating over items instead of indices.