Selenium find_element_by_css_selector not finding element - python

I'm working on a personal project to automate the joining of a Twitch channel through Selenium using Python. This is my first time using Selenium and I've run into an issue that I can't seem to resolve.
When joining a channel that is flagged as containing mature content, it requires a user to click a button to start watching the stream. My program works great until the button needs to be clicked.
The HTML for button appears as:
<button class="ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jnsGXs ksFrFH" data-a-target="player-overlay-mature-accept"><div class="ScCoreButtonLabel-sc-lh1yxp-0 evtFDZ"><div data-a-target="tw-core-button-label-text" class="Layout-sc-nxg1ff-0 eZactg">Start Watching</div></div></button>
Since the class name appear to be dynamically generated, I planned on the using the "data-a-target" as the selector. Looking at Selenium's docs, it appeared that the find_element_by_css_selector function is what I need to use. I played around in the Chrome developer tools and was about to select the button using: document.querySelector('button[data-a-target="player-overlay-mature-accept"]'), so I was confident that it would work with Selenium.
I made the selection in my code with Selenium using the following code:
mature = driver.find_element_by_css_selector('button[data-a-target="player-overlay-mature-accept"]')
Unfortunately, Selenium doesn't seem to be able to find it and returns the error message:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[data-a-target="player-overlay-mature-accept"]"}
(Session info: chrome=97.0.4692.71)
Am I doing something blatantly wrong? I've spent a good amount of time trying different things but nothing seems to work.

Maybe this:
mature = driver.find_element_by_xpath("//button[#data-a-target='player-overlay-mature-accept']")
or
mature = driver.find_element_by_xpath("//button[contains(#data-a-target, 'mature')]")

Related

Python Selenium how to interact with a specific button on a web page

Hi i'm trying to interact with this web element in the following link https://www.gurufocus.com/stocks
Here you can find the image
I want to set it to 100 so that i can visualize 100 stocks at the time i've tried to interact with it by using the following code
wd.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/section[2]/main/div/div[1]/div[3]/div[1]/span/div[2]").click()
wd.find_element_by_xpath("/html/body/div[2]/div[6]").click()
But only the first line works the second line gives me the following error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='el-popover-2775']"}
(Session info: chrome=106.0.5249.119)
It is not able to locate it i have also tried with the method find element_by_id but still not able to locate the button that sets the number on 100 This one does anyone has an other solution? Thanks in advance let me know if you need more information i will answer as soon as possible.
you can select the dropdown like this:
from selenium.webdriver.common.by import By
driver.find_element(By.CLASS_NAME, "aio-tabs-button").click()
and select 100 like this:
driver.find_element(By.XPATH, "//div[#class='item' and contains(text(), '100')]").click()
However, I'd recommend using the API and a package like requests instead of simulating the browser.

Is there any way to click on "plain text" using selenium?

Apologies if this question was answered before, I want to click on an area in a browser with plain text using Selenium Webdriver in python
The code I'm using is:
element_plainText = driver.find_elements(By.XPATH, '//*[contains(#class, "WgFkxc")]')
element_plainText.click()
However this is returning "ElementNotInteractableException". Can anyone help me out with this?
Selenium is trying to be helpful here, by telling you why it won't click on the element; ElementNotInteractableException means it thinks that what you're trying to click on isn't clickable.
This usually happens because either:
The element isn't actually visible, or is disabled
Another element is "overlapping" the element, possibly invisibly
You're clicking something Selenium thinks won't do anything, like plain text
There's two things I'd try to get around this. Firstly, Actions. Selenium has an Action API you can use to cause specific UI events to occur. I'd suggest finding the co-ordinates of the text, then making Selenium click those co-ordinates instead of telling it to click the element. Read more about that API here.
Secondly, try clicking it with Javascript, using a Javascript Executor. That can often give you the same outcome as using Selenium directly, without it being so "helpful".

Selenium XPath isn’t working: Unable to locate element error

I'm trying to make a Instagram bot that logs in, opens your profile etc. Everything works fine until one anchor tag:
self.driver.find_element_by_xpath("/html/body/div[1]/section/main/div/ul/li[3]/a").click()
I also tried the shorter way of writing this:
self.driver.find_element_by_xpath("//a[contains(#href,'/Obserwowani:')]").click()
and also tried searching by id.
This is a snippet of my function:
self.driver.find_element_by_xpath("/html/body/div[1]/section/main/section/div[3]/div[1]/div/div/div[2]/div[1]/div/div/a").click()
sleep(15)
self.driver.find_element_by_id("/html/body/div[1]/section/main/div/ul/li[3]/a").click()#not working
sleep(4)
scrollbox = self.driver.find_element_by_xpath("/html/body/div[5]/div/div/div[2]"
Can somebody tell me what is wrong please? Even sleep() doesn't work.

selenium cant find element - no such element

I have this website where I want to get the element post-title.
I tried driver.find_element_by_class_name("sc-1di2uql-1 vYcWR") but that just returns: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".sc-1di2uql-1 vYcWR"}
I have seen similar posts where the problem was that the searched element was in an iframe but I cant see anything like that here. Then again I started to learn about webscraping about a week ago so theres probably a lot I dont know.
I had a similar issue with selenium before. My problem was that the website would rotate class names, so every time you access the page they would be different. Judging by the way the class names are on your website I think you have the same issue. If this is the case, you are just trying to look for something that is not there. What I did to solve this problem was using driver.get_element_by_xpath(“PASTE FULL XPATH OF THE ELEMENT”) instead of finding the element using its css class.
To get the full path of an element: in the developer tools, right click on the element and click copy FULL XPATH.

How to select radio button using selenium

I am trying to click this radio button using selenium and python.
<input type="radio" name="tweet_target_1" value="website" class="tweet-website-button radio-selection-validate serialize-me newline-before field-order-15">
I have
website = driver.find_element(name="tweet_target_1")
website.click()
but it's not allowing me to click it. How can I click using a combo of name, value or class, value etc.?
Is there a good source of info about how to use selenium? Because most of what I've found is on java and I'm using python.
EDIT: using XPATH
I tried
website = driver.find_elements(By.XPATH, "//form[#id='dmca_form' and #class='twitter-form custom-form']/div[20][#class='list-container']/div[1][#class='list-item']/div[7][#class='clearfix inf-tweet init-hide']/div[#class='input']/ul[#class='options']/li[2]/label/input[#class='tweet-website-button radio-selection-validate serialize-me newline-before field-order-15']/")
website.click()
I keep getting
AttributeError: 'list' object has no attribute 'click'
I know this comes a little too late perhaps, but I joined just recently.
Tip: Use, Firebug and with it Firepath. Locate the radio button and find out the xpath for the element in question.
website = driver.find_element_by_xpath(".//**")
website.click()
OR
website = driver.find_element_by_xpath(".//**").click()
This should work all the time you try. Also, just using from selenium import webdriver
should make the click() function work correctly.
I'm not sure where you found the documentation that said you could call find_element like that, but you should either be doing driver.find_element_by_name("tweet_target_1") or driver.find_element(By.NAME, "tweet_target_1") (having first imported By of course). Also, Selenium Java code is pretty easily convertible to Python code; it follows a few pretty simple transformation rules, and if you still have questions, all the code for the library itself will also be on your machine to look at.

Categories

Resources