Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

Selenium(PYTHON) How to manipulate attributes

So this is more of a conceptual question, but im having trouble understanding whether or not i can manipulate the properties of a class, i understand how to navigate through the elements, but what if i want to in my case, make the element hidden, in the code below I have a for loop run through a bunch of coins that are classes that constantly dis/re appear on the screen so i have to count them before i can run through them all, hence 'count = len' but i made a print statement that even shows that the code runs through all the indexes properly, but they wont hide on the screen, what am i doing wrong

count = len(driver.find_elements_by_class_name('coinlist'))


for x in range(count):

    x += 1
    flip = driver.find_element_by_xpath(f"//ul[@class='coinlist']/div[{str(x)}]")
    flip.isdisplayed() = false
question from:https://stackoverflow.com/questions/65837695/seleniumpython-how-to-manipulate-attributes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to use driver.execute_script() with JS to modify the DOM.

count = len(driver.find_elements_by_class_name('coinlist'))

for x in range(count):
    
    x += 1
    flip = driver.find_element_by_xpath(f"//ul[@class='coinlist']/div[{str(x)}]")
    driver.execute_script("arguments[0].setAttribute('style','visibility:hidden;');", flip)

Should do the trick


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...