Selenium Twitter Buttons Not Clicking using xpath and css selector

Viewed 403

so I'm trying to make a bot for Twitter to press the reply button on a tweet.

I've used xPath, full xPath, and CSS Selector and I can't seem to get selenium to click on the button.

I've tried:

driver.find_element_by_xpath("//*[@id='react-root']/div/div/div[2]/main/div/div/div/div/div/div[2]/div/section/div/div/div[1]/div/div/article/div/div/div/div[3]/div[5]/div[1]/div/div/div/svg").click
driver.find_element_by_xpath("//div[@aria-label='Reply']").click
driver.find_element_by_css_selector("#react-root > div > div > div.css-1dbjc4n.r-18u37iz.r-13qz1uu.r-417010 > main > div > div > div > div > div > div:nth-child(2) > div > section > div > div > div:nth-child(1) > div > div > article > div > div > div > div:nth-child(3) > div.css-1dbjc4n.r-1oszu61.r-2sztyj.r-1efd50x.r-5kkj8d.r-18u37iz.r-h3s6tt.r-a2tzq0 > div:nth-child(1) > div").click
3 Answers

I see an error in your locator.
Try this:

driver.find_element_by_xpath("//div[@aria-label='Reply']").click

UPD
You should use this

//div[contains(@aria-label,'Reply')]

The reply icon is in SVG, try the below xpath :

//div[contains(@aria-label, 'Reply')]//descendant::*[local-name()='svg']

However, There are 4 buttons I can see in my twitter handle. Probably you would need to use find_elements to store them in a list.

Something like this :

reply_buttons = driver.find_elements(By.XPATH, "//div[contains(@aria-label, 'Reply')]//descendant::*[local-name()='svg']")
for reply in reply_buttons:
    if "Your condition here"
        reply.click()

If you look closely in the parent div tag of reply button, you can find a div tag with unique identifier as - data-testid="reply" Looking at the structure, I would recommend to grab this div using the attribute mentioned. And then nest to children div tags and then click on it. Using such absolute xpath as you have posted, would not be the best thing to do.

This is the potential xpath for listing all the reply buttons. Then you need to find a way to click on that particular reply button.

//div[@data-testid="reply"]/div/div/div
Related