Home > other >  Facing issue with selenium when I try to use "By.CSS_SELECTOR"
Facing issue with selenium when I try to use "By.CSS_SELECTOR"

Time:10-01

I'm trying to build a script, that can click on the Facebook group category "join" button, when certain conditions are met.

The script is already able to navigate "https://www.facebook.com/search/groups/?q=nature_lover" path using selenium.

Image: https://i.stack.imgur.com/3QJhy.png

After navigating to that path I used this code to handle, each group component data.

all_group_elements = self.driver.find_elements(By.CSS_SELECTOR, "div[role=article]")

for group_element in group_elements:
   
   group_name = str(element.text.split('\n')[0])
   group_button = str(element.text.split('\n')[-1])

   if group_button=="Join":
       group_button_target = f"Join Group {group_name}"
   if group_button=="Follow Group":
       group_button_target = f"Follow Group {group_name}"

   # I used this code to target and click the "join" button. 
   self.driver.find_element(By.CSS_SELECTOR, f"div[aria-label={group_button_target}]").click()

I'm also using "WebDriverWait" in the script. What is the issue here?

CodePudding user response:

Your issue is with f"div[aria-label={group_button_target}]"

That translates to something like "div[aria-label=Join Group NAME]"

That's a problem, because the value of the attribute contains spaces and you need quotes around the value if there are spaces.

Eg:

Bad: 'TAG[ATTRIBUTE=SOME VALUE]'

Good: 'TAG[ATTRIBUTE="SOME VALUE"]'

Those quotes are important if the value contains spaces. You may want to change that line to:

self.driver.find_element(By.CSS_SELECTOR, f'div[aria-label="{group_button_target}"]').click()
  • Related