0

I need to find and store the location of some elements so the bot can click on those elements even the if page changes. I have read online that for a single element storing the location of that element in a variable can help however I could not find a way to store locations of multiple elements in python. Here is my code

comment_button = driver.find_elements_by_css_selector("svg[aria-label='Comment']")
for element in comment_button:
    comment_location = element.location

sleep(2)
for element in comment_location:
    element.click()

this code gives out this error:

line 44, in <module>
element.click()
AttributeError: 'str' object has no attribute 'click'

Is there a way to do this so when the page refreshes the script can store the locations and move on to the next location to execute element.click without any errors?

I have tried implementing ActionChains into my code

comment_button = driver.find_elements_by_css_selector("svg[aria-label='Comment']")

for element in comment_button:
    ac = ActionChains(driver)
    element.click()
    ac.move_to_element(element).move_by_offset(0, 0).click().perform()
    sleep(2)

    comment_button = driver.find_element_by_css_selector("svg[aria-label='Comment']")
    comment_button.click()

    sleep(2)

    comment_box = driver.find_element_by_css_selector("textarea[aria-label='Add a comment…']")
    comment_box.click()

    comment_box = driver.find_element_by_css_selector("textarea[aria-label='Add a comment…']")
    comment_box.send_keys("xxxx")

    post_button = driver.find_element_by_xpath("//button[@type='submit']")
    post_button.click()

    sleep(2)

    driver.back()
    scroll()

However this method gives out the same error saying that the page was refreshed and the object can not be found.

selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of <svg class="_8-yf5 "> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
Egco
  • 19
  • 6

1 Answers1

0

Edited: Assuming No. of such elements are not changing after refresh of page, you can use below code

commentbtns = driver.find_elements_by_css_selector("svg[aria-label='Comment']")
for n in range(1, len(commentbtns)+1):
    Path = "(//*[name()='svg'])["+str(n)+"]"
    time.sleep(2)
    driver.find_element_by_xpath(Path).click()

You can use more sophisticated ways to wait for element to load properly. However for simplicity purpose i have used time.sleep.

rahul rai
  • 2,260
  • 1
  • 7
  • 17
  • Thank you for the suggestion however this method creates another problem. In a single page there are multiple `` elements and I need to click on them from top to bottom. Using this method the bot only clicks at the first ` it encounters from the top of the page and never moves on to the next element. – Egco Aug 15 '20 at 19:37
  • Just edited my question to give more information about the problem. – Egco Aug 15 '20 at 19:38
  • So we can assume number of such elements present at the first time page load is consistent. i.e. there is no change in number of elements after each refresh. Please see edited answer. – rahul rai Aug 15 '20 at 19:58
  • I have tried your solution however it didn't work I am getting this error: `TypeError: can only concatenate str (not "int") to str` – Egco Aug 16 '20 at 08:45
  • Try now. Converted int to String uisng str function. – rahul rai Aug 16 '20 at 11:14
  • `selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "svg[aria-label='Comment'][0]" is invalid: InvalidSelectorError: Document.querySelector: 'svg[aria-label='Comment'][0]' is not a valid selector: "svg[aria-label='Comment'][0]" ` The error message it gives is strange I am sure that selector is valid. No help with this code either. I think the thing I am trying to do is well beyond the capabilities of the webdriver. :( – Egco Aug 17 '20 at 04:02
  • @Egco No certainly its not beyond webdriver. It might be beyond me or yourself. You can check updated Answer. I have used xpath instead of css selector ( I have executed this script and worked ). In rhe mean time i will run with css selector as well and check what is issue. – rahul rai Aug 17 '20 at 04:47