Reputation: 55
I had a similar issue the other day and I resolved this by using the xpath as the element I was looking for was not in the original URL
I am trying to make a script to unfollow somebody on instagram and I am able to get to their profile page - it is just a matter of locating the button. I have tried using xpath and css selector
driver = webdriver.Chrome('C:\*\chromedriver.exe')
driver.get("https://www.instagram.com/accounts/login/")
elem2 = driver.find_element_by_name("username")
elem2.send_keys('*')
elem3 = driver.find_element_by_name("password")
elem3.send_keys('*')
elem3.send_keys(Keys.ENTER)
while True:
try:
elem4 = driver.find_element_by_css_selector(".-Cab_, .bIiDR")
elem4.click()
break
except Exception:
pass
names = ['chucknorris']
for i in names:
elem5 = driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input')
elem5.send_keys(i)
time.sleep(1)
while True:
try:
elems = driver.find_elements_by_class_name("yCE8d")
elems[0].click()
break
except Exception:
pass
## while True:
## try:
## elemL = driver.find_elements_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[2]/div/span/span[1]')
## except Exception:
## pass
#This part was just so I got the error message rather than infinite loop of nothing
time.sleep(4)
elemL = driver.find_element_by_xpath('//div[@id="react-root"]/section/main/div/header/section/div[2]/div/span/span[1]/button')
elemL.click()
'elemL' is the desired button element
Upvotes: 0
Views: 377
Reputation: 406
Your original xpath failed for 2 reasons.
Another option would be to just do this (if you are confident the text of the button won't change too often:
driver.find_element_by_xpath('//*[@id="react-root"]//main//button[.="Following"]')
Upvotes: 1
Reputation: 55
My answer was to use driver.find_element_by_css_selector('.-fzfL') which was the one which had background color attrib etc (really not sure if that's important)
Upvotes: 0