Home > other >  clicking a button in selenium
clicking a button in selenium

Time:01-11

i'm trying to get my web driver to click a button to go to the next page located in the bottom right corner of the page, but it seems that no matter what type of selector i choose it wont do anything, here's the button

<button  aria-label="Laman berikutnya"><svg  viewBox="0 0 24 24" width="24" height="24" fill="var(--N400, #6C727C)" style="display: inline-block; vertical-align: middle;"><path d="M9.5 17.75a.75.75 0 01-.5-1.28L13.44 12 9 7.53a.75.75 0 011-1.06l5 5a.75.75 0 010 1.06l-5 5a.74.74 0 01-.5.22z"></path></svg></button>

screenshot of the button code

link to the page: link to the page

my current code to get the webdriver to click the button is

driver.find_element("class name","css-1eamy6l-unf-pagination-item").click()
or
driver.find_element("css selector","button[aria-label='Laman berikutnya']").click()

i tried to use xpath too and it didn't work either, any idea?

CodePudding user response:

maybe it's would be like this if you are using a recent update of selenium

For CSS Selector

button = driver.find_element(By.CSS_SELECTOR,".fa.fa-sign-in.btn-primary")

button.click()

For html tag name

sign_up_button = driver.find_element(By.TAG_NAME,"button")
sign_up_button.click()

CodePudding user response:

You have correct locator, by css selector with value button[aria-label='Laman berikutnya'].

But the problem is you need scroll the web page to trigger this element until appear.

Add some logic like below, put scroll function inside loop and check with driver.find_elements* as a indicates that it appears or not:

driver.get(link to the page)

y=500
while len(driver.find_elements(By.CSS_SELECTOR, "button[aria-label='Laman berikutnya']")) == 0:
    driver.execute_script("window.scrollTo(0, "  str(y)  ")")
    y =500

driver.find_element(By.CSS_SELECTOR, "button[aria-label='Laman berikutnya']").click()

Note : You need to count the number of loops to prevent if the element never appears.

  • Related