how can I get the value in this page in python selenium - python

I am trying to get the value from webpage using selenium base on python. However, I do not get the point about how to make it. The value I want to catch is in the red mark side of below picture. Please help me to figure out this. Thank you very much!
Please click here to check the picture

There are few ways (By css,by xpath...) I recommend you reading this page: http://selenium-python.readthedocs.io/locating-elements.html
You also need some understanding about html, css and the DOM but the simplest way to get the value you want is by xpath (But its the slowest)
Open your web browser lets say chrome, right click on the element you want and inspect. It will open developer tools and you will have the html line selected already, right click it again-> copy -> copy xpath
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver')
driver.get("http://www.web.com")
driver.find_element_by_xpath(the_xpath_you_copied_before_from_dev_tools).text

Related

Selenium find a element with no class, div, text (Python)

I want to make a automatic google login with selenium but i cannot find the elements, the buttoenter image description heren "Next", because the class
is modified each time when we come to start a browser with selenium, or when we reset the login page and does not have Id, but the button is in a div that includes just this button,
I would like someone to help me find a solution to find how I can use this button in order to click it to skip the page where you have to put your email address to skip to the password
i would like to use css selector
(Google Chrome the browser I use)
I code with Selenium 4.2.0 on Linux Unbuntu
driver.find_element(By.CSS_SELECTOR, 'button[class="VfPpkd-LgbsSe VfPpkd-LgbsSe-OWXEXe-k8QpJ VfPpkd-LgbsSe-OWXEXe-dgl2Hf nCP5yc AjY5Oe DuMIQc LQeN7 qIypjc TrZEUc lw1w4b"]').click()
That will click the 'Next' button on Google login. Good luck getting any further though. Google seems to block logging in on Chromium.
You can either use jsname as an alternative for finding the element
I found a similar question here How can I inspect element in to div with jsname?
I think the person is doing pretty same thing
Hope this helps :)

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".

Scroll down section of webpage with selenium/python

I'm trying to scroll down a specific div in a webpage (ticker box in facebook) using selenium (with python).
I can find the element, but when i try to scroll it down using:
header = driver.find_element_by_class_name("tickerActivityStories")
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', header)
Nothing happens.
I've found a lot of answers, but mostly are for selenium in java, so i would like to know if it's possible to do it from python.
I've found a lot of answers, but mostly are for selenium in java
There is no difference, selenium has the same api in java and python, you just need to find the same function in python selenium.
First of all check if your JS works in the browser(if not, try scroll parent element).
Also you can try :
from selenium.webdriver.common.keys import Keys
header.send_keys(Keys.PAGE_DOWN)

How Do I Use Selenium WebDriver To Click A Non Anchor Tag?

I am using Selenium WebDriver to do automated testing of a website. I have been successful in clicking through numerous menus and links to a point.
At one point the website I am working with generates links that look like this:
<U onclick="HourglassSubmitItem(document.all('PageName').value, '00000001Responsibility Code')">Responsibility Code</U>
I am trying to use the .click functionality of the webdriver to click this link with no success.
Using this:
page.find_element_by_xpath("//u[contains(text(),'Responsibility Code')]")
successfully finds the U tag above. but when I add .click() to the end of this xpath, the click is not performed. But it also does not generate an error. So, my question is can Selenium be used to simulate clicks on an HTML tag that is NOT an anchor () tag? If so, how?
I will also say that I do not have control over the page I am working with, so changing the to is not possible.
I would appreciate any guidance the Community could provide.
Thank You for you help,
Chris
Sometimes using JavaScript could solve the "clicking" issue:
element = page.find_element_by_xpath("//u[contains(text(),'Responsibility Code')]")
page.execute_script('arguments[0].click();', element)
You can prefer JavaScript in this case.
WebElment element = page.find_element_by_xpath("//u[contains(text(),'Responsibility Code')]")
JavaScriptExecutor executor = (JavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", element);

Find CSS selector for popup menu item for Selenium in Python 3

I'm attempting to make a DeviantART Llamabot for a friend as my first Selenium project with Python 3. I have the bot 99% working except for being able to find the "give llama" button.
The problem seems to be that the menu the button appears under is a popup and I can't just right click and select "copy css selector" in Firefox. As soon as I inspect the element for the give menu the menu closes and the html changes.
I've managed to take a screen shot of a random sample page of the code so I can even see what's there. I've tried learning how CSS selectors work from scratch myself and I've managed to find every nested element EXCEPT the actual items in the list. I've tried looking for Nth child and using the ">" operator. I've attempted searching by class name, name, Xpath, link name, partial link name, nothing has worked.
I've read about this problem inspecting popup elements elsewhere and the suggestions are effectively to write an HTML parser or something to copy the entire html code as it changes and then select it from your copy. I'm not going to do that for this project. It's entirely too much work unless I absolutely have to for some reason.
Honestly at this point I don't even care anymore and I just want someone to outright just tell me what to type in so I can finish this project. This is the screenshot I managed to get. I'm looking for the item highlighted in blue.
Since my code was requested for clarification here it is
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary)
Deviant = browser.get("http://www.deviantart.com/random/deviant")
GiveMenu = browser.find_element_by_css_selector(".i47")
GiveMenu.click()
#GiveLlama = browser.find_element_by_css_selector("")`
Everything except the last line works which is why the last line is commented out until I can figure out what to put in there. No matter what I've tried, including the examples provided by the people answering this question so far, I either get a no such element error or an illegal syntax error.
You can use the xpath selector, in chrome you can use rigth click and click on copy > copy Xpath, or the element selected on your inspector is "div.popup2 .blockmenu a.f.givellama"
You should click the "Give" button first, when the pop up dialog opens, inspect the "Give a Llama Badge" element and identify the xpath. Here is a screen shot.
screen shot to locate "Give a Llama Badge" button
You can find that, the xpath to locate "Give a Llama Badge" button(Should click the Give button first to locate this element).
//a[#class='f givellama']
The xpath to locate "Give button"
//a[#href='#give-give-give']/span[text()='Give']
As you pointed out, when the popup dialog opens, if you try to test the xpath in the browser console via "Firepath" or others, when you type "Enter" or click the left mouse, the popup windows is closed. But don't worry about this, since you already identified the xpath locator, you can debug it in your scripts.

Categories

Resources