selenium cant find element - no such element - python

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.

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.

Selenium find_element_by_css_selector not finding element

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')]")

Unable to locate button in selenium

I am trying to get an element from a website and I would get this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div[3]/div[1]/div[1]/button"}
I got the element by copying the XPath in inspect element and made sure the element exists and is clickable. Here is my code:
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[1]/div[1]/button").click()
Here is the HTML code:
<button class="styled joinRound" data-text="joinGame">Join game</button>
How can I fix this?
It may be easier to test if you include the URL you’re working with.
You can try finding it by text instead:
driver.find_elements_by_xpath("//*[contains(text(), 'Join game')]").click()
Click button by text using Python and Selenium
This may also help. It’ll record your clicks and may give you the right path you need.
https://chrome.google.com/webstore/detail/katalon-recorder-selenium/ljdobmomdgdljniojadhoplhkpialdid?hl=en-US

Python Selenium Cant Find Elements Of Dropdown menu

Im a beginner in python and selenium, Ive been trying to make selenium click on the dropdown menu. Ive tried alot of solutions but none worked.
picture
This is my code
driver.get('chrome://settings/content/siteDetails?site=https%3A%2F%2Fmeet.google.com%3A443')
time.sleep(3)
microphone = driver.find_element_by_xpath("//select[#aria-label='Microphone']")
This is the error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate
element: {"method":"xpath","selector":"//select[#aria-label='Microphone']"}
Is there anything i did wrong?
This element is located inside multiple shadow-roots.
See here for more details how to deal with shadow DOM.

Python selenium - cant find element

I am not completely new to selenium but I cannot work this out. I am supposed to do a basket automation and I have troubles at the first page.
I am supposed to fill three input boxes at first to proceed to the actual order but I fail at the first input. We have unique ids for almost every input so it should not be too hard to find it but somehow it is.
<input class="form-input--text" data-validitytext-empty="Položka je povinná.<br>Prosíme, vyplňte ji." data-validitytext-invalid="Položka má špatný formát.<br>Prosíme, opravte ji." data-label-selector="closest(.form-item--required)->find(label.form-label)" id="surname003" name="Prijmeni" type="text" value="" data-form-required="true" xpath="1">
This is how the first input is described with id of "surname003" so my first try was to do this
driver.find_element_by_xpath("//*[#id='surname003']").send_keys("text")
Then I tried to access it with id
driver.find_element_by_id("surname003").send_keys("text")
Nothing from this was working so I tried to get the full xpath of it but that did not work.
This is how the code looks as a whole
driver = webdriver.Chrome(executable_path=r"C:\Users\KDK\Desktop\Selenium setup\chromedriver.exe")
driver.get(URL2)
driver.maximize_window()
driver.implicitly_wait(10)
driver.find_element_by_xpath("//*[#id='surname003']").send_keys("test")
driver.find_element_by_xpath("//*[#id='phone03']").send_keys(telefon)
driver.find_element_by_xpath("//*[#id='email03']").send_keys(email)
This is the error I get
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
This is the page I am working with :
https://www.nev-dama.cz/zima/rakousko/lungau/residence-carpe-solem-mariapfarr/rezervaceNova?termin=2021-01-10&typologie=[26225]&delka=3
I am thinking if the issue of locating the element is somehow connected to the fact that you can switch between two "windows" in the reservation page. You can switch between "Not binding reservation" and "Buy online" and I dont know if that makes it somehow harder to get to the element.
I hope someone can help me with this. If something is not clear please ask me and thanks for reading and thanks in advance for some answers! :)
They have few inputs with the same id and first is from another from. But it mustn't raise NoSuchElementException.
Try XPATH like that
//div[#id="koupit-online"]//*[#id='surname003'].
You need to add this part //div[#id="koupit-online"] to all all xpath or use as context
form = driver.find_element_by_xpath("//div[#id='koupit-online']")
form.find_element_by_xpath(".//input[#id='surname003']").send_keys("test")
form.find_element_by_xpath(".//input[#id='phone03']").send_keys(telefon)
form.find_element_by_xpath(".//input[#id='email03']").send_keys(email)
Second problem can be chromedriver. When i used chrome with selenium i got this exception without reason. I changed browser to firefox and geckodriver and this never happend again

Categories

Resources