new to selenium

116 views
Skip to first unread message

amir shoob

unread,
May 6, 2021, 3:21:29 AM5/6/21
to Selenium Users
Hello,
I'm trying to reach twitchtv website and click the follow using selenium.

My problem, is that I do not have id nor name so I can not reach this element and click on it.

Any ideas ? 

Arvind Sah

unread,
May 6, 2021, 4:09:24 AM5/6/21
to seleniu...@googlegroups.com
Hello,

Please try spending some more time with finding xpath and css. I am sure you will be able to fine the solution for this problem.

If still you are not able to identify the locator please share the snippet of the dom so that people can assist you.

Thanks and regards
Arvind sah

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/4f7d0d6d-9c01-46e5-8cc9-cd60ed3d880an%40googlegroups.com.

amir shoob

unread,
May 7, 2021, 11:29:30 AM5/7/21
to seleniu...@googlegroups.com
Hello.

I have just spent some time reading about the material you gave me (sorry I'm more of a low level type).

I wanted to try the Xpath so I used this command:
elem = driver.find_element(By.XPATH, '//button') # which give the first button

It gives the first element which I do not need so I used the  find_elements function to get the list of all the buttons.

Although when I try to locate it specifically I get an exception says unable to locate element (I can not find by id because it is generated everytime differently).
my try:
elem = driver.find_element_by_css_selector('button.ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button')
and I tried this also:
elem = driver.find_element(By.XPATH, '//button[@class=" ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button"]')

And I'm trying to press the follow button and I think this is the code related to it and my Python little script:
image.png

image.png

‫בתאריך יום ה׳, 6 במאי 2021 ב-11:09 מאת ‪Arvind Sah‬‏ <‪arvind...@gmail.com‬‏>:‬

amir shoob

unread,
May 7, 2021, 11:32:07 AM5/7/21
to Selenium Users
Hello.

I have just spent some time reading about the material you gave me (sorry I'm more of a low level type).

I wanted to try the Xpath so I used this command:
elem = driver.find_element(By.XPATH, '//button') # which give the first button

It gives the first element which I do not need so I used the  find_elements function to get the list of all the buttons.

Although when I try to locate it specifically I get an exception says unable to locate element (I can not find by id because it is generated everytime differently).
my try:
elem = driver.find_element_by_css_selector('button.ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button')
and I tried this also:
elem = driver.find_element(By.XPATH, '//button[@class=" ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button"]')

And I'm trying to press the follow button and I think this is the code related to it and my Python little script
unnamed (1).png:
unnamed.png
ב-יום חמישי, 6 במאי 2021 בשעה 11:09:24 UTC+3, arvind...@gmail.com כתב/ה:

Arvind Sah

unread,
May 8, 2021, 10:31:49 AM5/8/21
to seleniu...@googlegroups.com
Hello Amir,

You are still required to spend some more time on finding the css and xpath locator.

Below are some of the improvements which are required.

1. CSS which was used is incorrect instead of this -"driver.find_element_by_css_selector('button.ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button')" it should have been this - "driver.find_element_by_css_selector('button.ScCoreButton-sc-1qn4ixc-0.ScCoreButtonPrimary-sc-1qn4ixc-1.jCxFvU.tw-core-button')".  // please notice that spaces are replaced with dot(.)

2. Xpath written in incorrect - you have put the extra space at the beginning of class name. It should be -   xpath= //button[@class="ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button"]

Post identifying the locator you can cross check the locator using develop tool itself  :
Developer tool-> Elements-> CNTRL+F -> paste the locator(xpath, css ...) -> if that is valid locator then you will be able to find the number matching nodes as shown in the below image

image.png


I could see that you are trying to locate the follow button, below are the few locators which can be used for locating follow btn.
image.png
-> xpath= //button[@class="ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button"] // mose likely to fail in long run because of the dynamic class name value
xpath= //button[@data-test-selector="follow-button"]
xpath = //*[contains(text(),'Follow')]
xpath= //div[@class='follow-btn__follow-btn']//button

-> css = button.ScCoreButton-sc-1qn4ixc-0.ScCoreButtonPrimary-sc-1qn4ixc-1.jCxFvU.tw-core-button // / mose likely to fail in long run
css = button[class='ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button']
css= div.follow-btn__follow-btn


Now, let's assume you wanna locate the search symbol which is at the top centre near to search into field, using the xpath "//button" I could see that there are 33 matching nodes available. We should look for some other properties (either to the parent or child nodes of the current element) which are unique for a given element. If we try to look at the some of parent nodes I could see that unique node "//div[@class='search-box']",  once you are unique node you can create your locator using the same like this "//div[@class='search-box']//button".

This link will help you to get some info for xpath locator technique - https://www.guru99.com/using-contains-sbiling-ancestor-to-find-element-in-selenium.html.

There are multiple other ways to locate the element which probably you can learn on the go.

Regards,
Arvind Sah


amir shoob

unread,
May 8, 2021, 5:29:12 PM5/8/21
to Selenium Users
That was really helpful

Thank you very much

ב-יום שבת, 8 במאי 2021 בשעה 17:31:49 UTC+3, arvind...@gmail.com כתב/ה:

amir shoob

unread,
May 9, 2021, 1:26:39 AM5/9/21
to Selenium Users
I'm trying to run the examples you gave me and it still:
Unable to locate element
even though I'm fixing the errors you mentioned
I've tried all (xpath and css)

Is there any chance it is OS dependent ?
I'm running on Windows and Selenium 3.141.0


ב-יום ראשון, 9 במאי 2021 בשעה 00:29:12 UTC+3, ‪amir shoob‬‏ כתב/ה:

Arvind Sah

unread,
May 9, 2021, 1:35:20 AM5/9/21
to seleniu...@googlegroups.com
Hello Amir,

No, locator strategy is independent of system of execution. Please share below items- 

1. Ui element snapshot which you are trying to locate.
2. Locator used ( xpath or css )  to locate the ui element along with snippet of the dom corresponding to the element.
3. What's the error you are facing when you tried the step2 locator?

Regards,
Arvind

amir shoob

unread,
May 9, 2021, 2:49:22 AM5/9/21
to Selenium Users
I'm trying to locate the follow button in https://www.twitch.tv/gmhikaru

 follow.png

I've tried many locators:
css:

'button.ScCoreButton-sc-1qn4ixc-0.ScCoreButtonPrimary-sc-1qn4ixc-1.jCxFvU.tw-core-button' // as you suggested earlier

button[class='ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button']

error:
css error.PNG

xpath:

//button[@class="ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jCxFvU tw-core-button"]

error:
xpath1.PNG

//button[@data-test-selector="follow-button"]

error:
xpath2.PNG

the code I'm using is:
twitchpng.PNG
ב-יום ראשון, 9 במאי 2021 בשעה 08:35:20 UTC+3, arvind...@gmail.com כתב/ה:

Arvind Sah

unread,
May 9, 2021, 5:37:32 AM5/9/21
to seleniu...@googlegroups.com
Hi Amir,

Can you please make sure that UI element is visible before you try to locate particular UI element. I suspect that you are trying to locate the element even before it's visible. There is no issues with the locator (xpath or css).

You can follow below ways to achieve visibility of UI element.
1. Use of explicit wait // recommended one
2. use of implicit wait
3. Thead.sleep / time.sleep

This link will help you to get some insight about the different waits - https://www.guru99.com/implicit-explicit-waits-selenium.html

Just to make you code work, you can put sleep of 10 secs just before locating the element.  Still I would recommend you to spend some more time understanding the basics, try searching for errors which you may face during script design, this will help you debug the issue.

Regards,
Arvind



Reply all
Reply to author
Forward
0 new messages