1

I am trying to automate deleting my old comments on posts I've made on FB. Im using Selenium with python to achieve this.

Hidden Delete Button

I am currently having trouble dealing with pressing the hidden buttons (1 & 2)

Hamburger button element

<div aria-expanded="false" aria-haspopup="menu" aria-label="More" class="oajrlxb2 gs1a9yip g5ia77u1 mtkw9kbi tlpljxtp qensuy8j ppp5ayq2 goun2846 ccm00jje s44p3ltw mk2mc5f4 rt8b4zig n8ej3o3l agehan2d sk4xxmp2 rq0escxv nhd2j8a9 pq6dq46d mg4g778l btwxx1t3 pfnyh3mw p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x tgvbjcpo hpfvmrgz jb3vyjys rz4wbd8a qt6c0cv9 a8nywdso l9j0dhe7 i1ao9s8h esuyzwwr f1sip0of du4w35lb lzcic4wl abiwlrkh p8dawk7l dwo3fsh8 pzggbiyp pkj7ub1o bqnlxs5p kkg9azqs c24pa1uk ln9iyx3p fe6kdd0r ar1oviwq l10q8mi9 sq40qgkc s8quxz6p pdjglbur" role="button" tabindex="0"><i class="hu5pjgll m6k467ps sp_D-6eZQK8GT3_1_5x sx_956c87"></i><div class="s45kfl79 emlxlaya bkmhp75w spb7xbtv i09qtzwb n7fi1qx3 b5wmifdl hzruof5a pmk7jnqg j9ispegn kr520xx4 c5ndavph art1omkt ot9fgl3s rnr61an3" data-visualcompletion="ignore" style="bottom: -8px; left: -8px; right: -8px; top: -8px;"></div></div>

Hamburger button XPath

//*[@id="mount_0_0"]/div/div[1]/div[1]/div[3]/div/div/div[1]/div[1]/div[4]/div/div/div/div/div/div[1]/div/div/div/div/div/div/div/div/div/div/div[2]/div/div[4]/div/div/div[2]/ul/li/div[2]/div/ul/li[2]/div/div/div[2]/div[2]/div[2]/div/div

Delete Button Element values

<span class="d2edcug0 hpfvmrgz qv66sw1b c1et5uql rrkovp55 a8c37x1j keod5gw0 nxhoafnm aigsh9s9 d3f4x2em fe6kdd0r mau55g9w c8b282yb iv3no6db jq4qci2q a3bd9o3v ekzkrbhg oo9gr5id hzawbc8m" dir="auto">Delete</span>

Delete Button XPath

//*[@id="mount_0_0"]/div/div[1]/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/div/div[1]/div/div[1]/div/div[2]/div[1]/div/div

After Inspecting the html code, I saw there are duplications with the class and the id, so i've used WebTest tool to figure out the XPath exact elements:

CSS Selector (for first hidden Hamburger button)

div[data-testid="Keycommand_wrapper_feed_story"] > div > div > div > div > div > div > div > div:nth-of-type(2) > div > div:nth-of-type(4) > div > div > div:nth-of-type(2) > ul > li:nth-of-type(2) > div:nth-of-type(1) > div > div:nth-of-type(2) > div > div:nth-of-type(2) > div > div

XPath (for the second Hidden delete button)

//span[text() = "Delete"]

CSS Selector (for the third popup delete button)

div[aria-label="Delete"][role="button"][style="transform: none;"]

So i've tried testing to see if im able to click just on the first hidden hamburger button, this is part of the code:

driver.findElement(By.xpath("//*[@data-testid='Keycommand_wrapper_feed_story']/parent::div")).click();

What am I doing wrong? does the fact they are hidden makes it unsolvable?

1

Try to use developer APIs to perform this kind of actions against public sites like Facebook.

For multiple reasons, logging into sites like Gmail and Facebook using WebDriver is not recommended. Aside from being against the usage terms for these sites (where you risk having the account shut down), it is slow and unreliable.

The ideal practice is to use the APIs that email providers offer, or in the case of Facebook the developer tools service which exposes an API for creating test accounts, friends and so forth. Although using an API might seem like a bit of extra hard work, you will be paid back in speed, reliability, and stability. The API is also unlikely to change, whereas webpages and HTML locators change often and require you to update your test framework.

See Selenium Worst Practices documentation

1
  • 1
    I'm am torn about X Y answers. When someone asks X and the answer is "X is bad, do Y" I usually don't look favorably on it. But here you literally have the people who make it saying X is bad. This answer is also great because even without the link it's spot on in explaining why it's bad and what the alternative is. That's how a "link only" answer should be. Would upvote twice if I could.
    – corsiKa
    Dec 29 '20 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.