Hack Forums
[Python] Need help with my selector in Selenium - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Need help with my selector in Selenium (/showthread.php?tid=6119997)



Need help with my selector in Selenium - Kαѕρer - Nov 6, 2020

I have no idea why it won't click on the button.

This is the code of the button:
[Image: 13Jm1Hi.png]

And this is my code:
Code
btnOrder = driver.find_element_by_class_name('js-shoppingcart-submit-button')
btnOrder.click()

Thanks.


RE: Need help with my selector in Selenium - Charger - Nov 6, 2020

You need the full class instead of just part


RE: Need help with my selector in Selenium - TheGoobr - Nov 6, 2020

Looks like you're using only "js-shoppingcart-submit-button" which is not the full name of the class. You should be using its full name that includes the part from the line above it


RE: Need help with my selector in Selenium - Kαѕρer - Nov 7, 2020

(Nov 6, 2020 09:54 PM)Charger Wrote:  You need the full class instead of just part

(Nov 6, 2020 10:57 PM)TheGoubr Wrote:  Looks like you're using only "js-shoppingcart-submit-button" which is not the full name of the class. You should be using its full name that includes the part from the line above it

What if I only know a part of the class name that returns? And other stuff changes cause of mobile/desktop?


RE: Need help with my selector in Selenium - Wayne - Nov 7, 2020

You need to find the element bu Xpath

//*[contains(@class, "js-shoppingcart-submit-button")]

This should work ^^^^^^^


RE: Need help with my selector in Selenium - Harley Quinn - Nov 8, 2020

(Nov 6, 2020 05:26 PM)Kαѕρer Wrote:  I have no idea why it won't click on the button.

This is the code of the button:
[Image: 13Jm1Hi.png]

And this is my code:
Code
btnOrder = driver.find_element_by_class_name('js-shoppingcart-submit-button')
btnOrder.click()

Thanks.

Code
driver.find_element_by_selector('SELECTOR_HERE').click()

is what i would use... or xpath. or, you could use the full class name not just the first portion.


RE: Need help with my selector in Selenium - Yesterday - Nov 12, 2020

Use xpath, here's a video on how to find it if you're unsure:
https://www.youtube.com/watch?v=jSiYpmvA50A

If the xpath changes depending on how the website is displayed, e.g pc/mobile, then use an if statement to decide which class you want selenium to use


RE: Need help with my selector in Selenium - Ayyyther - Nov 27, 2020

If you ever have problems with Selenium, be it user-error or the site doing something tricky to make your job harder, you can just inject Javascript into the page.

Code
driver.execute_script(f'''
        {consts.SET_COUNTRY};
        document.getElementsByName("{consts.DOB_YEAR}")[0].value={random.randint(1975, 2001)};
        document.getElementsByName("{consts.DOB_MONTH}")[0].value={random.randint(1, 12)};
        document.getElementsByName("{consts.DOB_DAY}")[0].value={random.randint(1,28)};
        {consts.CONT_BUTTON_GO}
    ''')

Code
# - Every page
CONT_BUTTON_GO = 'document.getElementById("flow-form-submit-btn").click();'