How can I get the text from a dialogue using selenium-python? - python

I want to crawl the dialogue text in a popup window. The problem is that after I triggered the link the window appears but it seems that the selenium driver cannot handle it automatically as I learned from other questions on this site by entering driver.window_handles.
The source of the trigger:
The value of len(driver.window_handles) is 1. I thought I can get the window element and then get the text via the get_attributes, fortunately I succeeded getting the element by
wd = driver.find_element_by_css_selector('div[node-type="repeat_list"]')
selenium.webdriver.remote.webelement.WebElement (session="f810cbbe-db43-4e8d-b484-664559ec8efc", element="{dd00e689-7991-44e9-85d3-76c69e79218f}")
But the sad thing is I don't know how to get all the stuff out from it since I don't know their attributes.
I'm not certain if it's a dialogue, a front end engineer told me that it looks like an animation. Anyway this is the source snippet:
PS: the browser is Firefox.
I thought it may violate the site's Acceptable Use Policy to crawl then I should hide some information. Sorry.

Once you have your parent element :
wd = driver.find_element_by_css_selector('div[node-type="repeat_list"]')
you can continue calling methods on this object, and in this order reach the children elements, you can use find element_by_xpath, or find element_by_class name, for example:
wd = driver.find_element_by_css_selector('div[node-type="repeat_list"]')
wd.find_element_by_class_name("list_box").find_element_by_class_name("list_ul").find_elements_by_class_name("list_li S_line1 clearfix")
and so on until you reach the desired element down the hierarchy and extract it's content as you wish.
I hope this helps!

Related

SELENIUM Can't access payment form/fields

https://squidindustries.co/checkout
checkout_cc_number = driver.find_element_by_id("number")
checkout_cc_number.send_keys(card_number)
When I try to input information into the card number field I get an error saying the element could not be located. I tried using time.sleep and driver.implicitly_wait when i first got to the page but both failed. Any ideas?
The element is in a frame (i.e. a webpage within a webpage). Selenium will look for elements in the page it has loaded and not within frames. That's the problem.
To solve this we just need a bit more code, which will tell Selenium to look in the frame.
The example you've given is several pages deep into a shopping cart, so I'm going to use a much more accessible example instead: the mozilla guide to iframes.
Here is some code to open that page and then click the CSS button within the frame:
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get(r"https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
time.sleep(5)
browser.switch_to.frame(browser.find_element_by_class_name("interactive"))
css_button = browser.find_element_by_id("css")
css_button.click()
browser.switch_to.default_content()
There are two lines that are important. The first one is:
browser.switch_to.frame(browser.find_element_by_class_name("interactive"))
That finds the frame and then switches to it. Once we have done that, any code that looks for elements will be looking in the frame and not in the page that we navigated to. That is what you need to do to access the number element. In your example the class of the frame is card-fields-iframe, so use that instead of interactive.
The second important line is:
browser.switch_to.default_content()
That reverts the previous line. So now Selenium will be looking for elements within the page that we navigated to. You'll want to do that after interacting with the frame, so that you can continue through the shopping cart.
have you tried getting the input element using the DOM? what happens if you do document.getElementById('number') ?
I ran into the same issue, and with checkouts, as you mentioned, all the iframe class names are the same. What I did was get all the iframes with the same class name as a list:
iframes = driver.find_elements(By.CLASS_NAME, "card-fields-iframe")
I then switched through the iframes referencing each one by its place in the list. Since there are only four fields in the checkout, the list is only 4 elements long, starting with [0].
driver.switch_to.frame(iframes[0])
number = driver.find_element(By.ID, "number")
if number.is_displayed:
number.send_keys("4000300040005000")
driver.switch_to.default_content()
It's important to note that switching back to the default content, using driver.switch_to.default_content(), before switching to the next frame is the only way I was able to make this work. The is_displayed function just checks to see whether the element is on the page or not.

How to find and click on specific element using selenium?

I'm starting in the world of python and I'm practicing creating things for my day to day. I decided to automate a routine task, but I can't get selenium to click on a specific place because it changes depending on how many columns I have and those columns always change order when I open a new login.
I thought about trying to find the specific xpath using the place name in the case "CORTEZ" and then find the "" specifies where I can click the button, but this "" is not inside the text "CORTEZ" it is in the same "line" or "group" (I don't know the correct term, forgive me) you will identify this in the image at the end of the text. I have no idea how to do this and I don't even know if it's possible as I said, I'm new to this world and I'm trying to learn little by little. I accept other subjects too
Most complete xpath I've ever tried too:
// table [# id = "accordionConvenio_Pane_0_content_GridView1"] / tbody / tr [3] / td / input
My current code:
def confirmarlocal(self):
try:
time.sleep(0.25)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,'//*[#id="accordionConvenio_Pane_0_content_GridView1_ctl05_btnParticipar"]'))).click()
except TimeoutException:
return
NOTE: I CANNOT DOWNLOAD THE HTML CODE THEN FOLLOW A PICTURE BELOW
Find the input based off it's class, title.
//input[#class='btnParticipar']
//input[#title='Confirmar']
If the problem consists in assembling the correct XPath to the desired tag, you can right-click the tag in the inspection menu, select "Copy" and then copy its XPath to get exactly the value you need, at least on Chrome.

Selenium unable to automate click on web element

I should start by saying that I'm not a web developer but I have been using Selenium with Python for a few weeks and think I've gotten the basics down.
What I have is a page with a weekly calendar on it. Image as follows:
What happens is that you can click on any of the coloured boxes which will bring up a register for a class. It features items that you can click on which bring up new information. The problem I have is that I can't click on the items - or, rather, I don't know how to automate the click. I have automated clicking with Selenium before in other situations.
That looks as follows:
As you can see, the calendar still appears in the background. That is to say, the item we click on doesn't take us to a new page but, I suppose, runs some kind of method which shows the register and populates it with data.
My problem is this: I want to automate the process so that I can click on each of these items then scrape the information. In order to do that I need to be able to automate the clicking for each of the items.
So what have I done? When I've done this before, I've searched through the web html for the relevant part and then grabbed the xPath to the element I needed. But here I can't do that. Why not? Well, firstly, I just can't find that element!
Take a look at this close up of the first column:
It's divided into columns, but then I'd expect the clickable area to be an element within that. As you can see, it's not. Furthermore, the clickable area is just the coloured box itself, but if you look closely you can see the element goes outside of that area. I have gone very close with my mouse cursor to see exactly what's clickable, and it definitely is just the coloured box.
So I've not been able to get the element at all.
I thought I might be able to just find out where we went after I clicked the button, but when I got the link address, it just said it was the same page with no differences.
I appreciate I'm asking quite a broad question here, but the problem is that I don't really know where to start. If someone could give me at least that much, I would be grateful. Like if I could just click on each of these one at a time... I've found where the populated data is so I could grab that without a problem.
Well, here's to hoping.
Edit: I should add that there are some JavaScript items (tag type script, type='text/javascript'). I presume that the answer is in there somewhere, but there is a lot of Javascript and I'm not adept at reading it. It's hard for me to tell what script does what. If I could at least figure out what script runs when I click the item then I think I'd be onto something, but I have no idea. Even that would help me.
I had encountered similar problem when scraping for Instagram followers in mobile view where it was floating box when showing the accounts followers name. The approach I took was identifying the name of floating dialog box and clicking the element in it. It might different in your case of html.
Trying looking at this link. Selenium Scroll inside of popup div
Hard to say without the HTML. Maybe try Katalon Recorder (chrome extension) and see if that can detect the xpath for you? It might also be you have to use some kind of javascript to invoke the method for the element

How to verify whether a certain word is present in the loaded page through WebDriver?

How can I verify if a user sees on the screen a word, for instance “book” or “Book” ( on the webpage like https://www.amazon.com/gp/gw/ajax/s.html?ie=UTF8&ref_=nav_logo ? A script should check everything what a user sees including images (“title” and “alt”). The script should return True or False. How can I check the whole page using assert True, assert False ? Has anybody written something like this in Python ?
Get the complete text of the page .
WebDriver driver = new FirefoxDriver();
String bodyText = driver.findElement(By.tagName("body")).getText();
//if the word you would like to check is "Book"
boolean isWordPresent = bodyText.contains("Book");
Maintain a list of the words you wish to verify on the page and loop through the list.
Yeah, I have written similar tests to make sure a page is properly loaded.
First of all, you need to get hold of those elements' identities from HTML, in order to achieve this, you can use Firefox plugin: Firebug or Chrome's developer mode to inspect elements.
Once you have got hold of your element's identity, you have a few options from here, you can either locate an element by Xpath or locate an element by Css selector, please go to w3school for more detailed tutorials.
When an element is located, using either find_element_by_xpath or find_element_by_css_selector, you can use element.innerText() function to get their texts, I think they are what you are after.
You can look into the entire HTML page, verifying if those texts you expect will appear.
Hope it can help you out a bit.

Mouse the focus to an object using selenium python

I am automating a website using selenium RC and python 2.7 on Ubuntu Linux. Here is what I need to do:
Go to the site http://borro.com.
Scroll down to the bottom of the page using key down native command
I need to hover the mouse on g +1
read the tool tip
click on the name that appears in the tool tip.
The problem I am having is -- I need the mouse to physically move there, wait for say 2 secs and then read the tool tip and click on the name
The mouse is not physically moving there and I think the focus is lost and it says element xpath not found.
We've solved a lot of our focus issues by sending a blank key to the element so it gets focused. In this case, you'd probably want to send blank key to the tooltip as soon as it appears. I'm familiar with webdriver but not RC, but RC should have something like send_key(element_xpath, " ") as well.
To get the tooltip's xpath, you can use firebug, and in the console, use something like
$x("//*[contains(text(), 'Publicly recommend this as')]")
to make sure this element is found and xpath is correct. I also recommend not using wildcard characters, so once you find the tooltip's xpath, try to replace the * by the actual element type.
Button Xpath and on hover on button xpath are given below
Actions builder = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("button"));
builder.moveToElement(tagElement).build().perform();
/html/body/div/div/table/tbody/tr/td/div
Try:
selenium.mouseOver("mylocator");

Categories

Resources