0

I have the xpath to follow a user on a website in selenium. Here is what I thought of doing so far:

followloop = [1,2,3,4,5,6,7,8,9,10]
for x in followloop:
    driver.find_element_by_xpath("/html/body/div[7]/div/div/div[2]/div[>>>here is where i want the increment to increase<<<]/div[2]/div[1]/div/button").click()

So where I stated in the code is where I want the number to go up in increments. Also as you see with the for loop Im' doing 1,2,3,4,5...can I code it more simply to be like 1-100? Because I don't just want 1-10 I will want it 1-whatever higher number.

I tried to just put x in where I want it but realised that python won't pick up that it's a variable I want to put in that slot and will just consider it as part of the xpath. So how do I make it put the increasing number variable number in there on each loop?

1
  • This type of xpath practice is not good to maintain. Please attach more information e.g. html code of the page, site url and what you want to extract from them. Groping answer is not efficient for both the asker and answerer! Commented Oct 16, 2015 at 13:51

3 Answers 3

4

You need to convert the index from the for loop into a string and use it in your xpath:

follow_loop = range(1, 11)
for x in follow_loop:
    xpath = "/html/body/div[7]/div/div/div[2]/div["
    xpath += str(x)
    xpath += "]/div[2]/div[1]/div/button"
    driver.find_element_by_xpath(xpath).click()

Also, there will generally be a neater/better way of selecting an element instead of using the XPath /html/body/div[7]/div/div/div[2]. Try to select an element by class or by id, eg:

//div[@class="a-classname"]
//div[@id="an-id-name"]
Sign up to request clarification or add additional context in comments.

10 Comments

Hi. It didn't work unfortunately. Nor did the otehr suggestions. Each time nothing happens and script completes in command prompt. I'd be open to suggestions on xpath alternatives however I was trying yesterday and couldn't figure out any others that work. Here is the code from firebug: gist.github.com/anonymous/5a49f4cc383553213a2d. I need to try with id since it looks simple but it was highlighting more than that area. Gotta test a bit more...
Your XPath must be incorrect. Can you provide us with more information? Can you view the browser in action?
The xpath is correct because the button works fine when I removed the x and just place a number.
The way I have converted an int into a string above is 100% correct. It is likely that the HTML structure of the page changes after the first button is clicked, hence the XPath no longer works. Your absolute XPath expression is very fragile - as suggested in my answer, it is better to select a nearby parent div by id or class.
Thing is though when I manually restart it and just change the number to add one in the script in the xpath each time it works to follow a different user so I doubt that is the case? I tried with id but it doesnt work since it is highlighting more than the follow button and clicks something else. but I was trying yesterday with the other options class etc and came up short for similar reasons hence why I went the xpath route. And yes Im looking at the browser as it's doing things. and with your code and others suggested nothing happens. The script just ends.
|
0

I would use a wildcard '%s' for your task and range() as indicated in previous comments:

for x in range(0,100): 
     driver.find_element_by_xpath("/html/body/div[7]/div/div/div[2]/div[%s]/div[2]/ 
     div[1]/div/button").click() % x

Comments

-1

Use a format string.

And use range() (or xrange() for larger numbers) instead. It does exactly what you want.

for x in range(10):
    driver.find_element_by_xpath("/html/body/div[7]/div/div/div[2]/div[%d]/div[2]/div[1]/div/button" % (x,)).click()

1 Comment

range(10) iterates over 0 - 9, OP lists 1 - 10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.