Home > Net >  Can t find the xpath for Following button instagram for selenium
Can t find the xpath for Following button instagram for selenium

Time:02-24

I try to get the xpath for the following button on instagram making an automate unfollowing soft. enter image description here

I found it just like this:

driver.find_element_by_xpath('//div[@]').click()

But i want to itterate over all ,,Following" Buttons , but like this is stuck at the first one! This is my Code:

fBody  = driver.find_element_by_xpath("//div[@class='isgrP']")  
for i in range(1, 1500):
            driver.find_element_by_xpath('//div[@]').click()
            #driver.find_element_by_xpath("//*[text()='Following']").click()
            print("Am apasat follow")
            sleep(5)
            
            driver.find_element_by_xpath('//button[@]').click()
            sleep(5)
            driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop   arguments[0].offsetHeight;', fBody)
            print("Ma bag la somn 1 min")
            sleep(2)
print("salut")

CodePudding user response:

Selenium does Not "like" empty or white spaces in the attributes.

I suggest using a CSS selector and using *= in order to find text contains:

driver.find_element_by_CSS('//div[class*="qF0y9"][class*="Igw0E"]').click();

Avoid using white or empty spaces and, underscores (_) and hyphens (-) for the element's attributes.

CodePudding user response:

I think the classes on the elements change as yours do not match with mine. Here is a more generic XPath that matches the "following" button.

//div//button[div[text()='Following']

When using this in a test I found it instantly failing unless I surrounded it with an explicit wait condition.

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div//button[div[text()='Following']"))).click();

Ill post my example when Instagram stops giving me connectivity issues.

  • Related