ravishankar chavare
ravishankar chavare

Reputation: 503

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/

Upvotes: 0

Views: 189

Answers (2)

PDHide
PDHide

Reputation: 19949

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)

Upvotes: 2

Libin Thomas
Libin Thomas

Reputation: 979

The author name and the text link "about" are specified in two different lines. Instead of just hiding the author's line using class name, I would suggest try hiding the whole span

Xpath for the same will be xpath = "/html/body/div/div[2]/div[1]/div[1]/span[2]"

If you want to retain the text "by", then try specifying separate variables to find the element of the two lines and hide/unhide them individually. This should not alter the structure of the html elements. enter image description here


Try this and let me know if it worked.

Upvotes: 0

Related Questions