Search code examples
pythonpython-3.xseleniumweb-scrapingwebdriver

position of element is changing after re-showing selenium element


I am performing same operation for author and about anchor tags

Code to find element

element = driver.find_element('class_name','author')

Code to hide element

driver.execute_script("arguments[0].style.display = 'None';", element)

code to show element

driver.execute_script("arguments[0].style.display = 'block';", element)

enter image description here

Question why the author name and about element position changed after re-showing same elements?

Website Link : https://quotes.toscrape.com/


Solution

  • https://www.w3schools.com/cssref/pr_class_display.asp

    block displays the element in next line. delete that property completely or use inline

    driver.execute_script("arguments[0].style.display = undefined;", element)
    

    or

    driver.execute_script("arguments[0].style.display = 'inline';", element)