Python Selenium button isn't being clicked - javascript

I am trying to scrape data from this website (https://www.ilcollege2career.com/#/) using python (selenium and beautiful soup).
The code I have is this:
driver = webdriver.Chrome('my file path')
driver.get('https://www.ilcollege2career.com/#/')
first_click = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="tutorial-modal"]/div/div/div/div[3]/button[1]')))
first_click.click()
second_click = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="tutorial-start-modal"]/div/div/div[2]/div[2]')))
second_click.click()
So my problem is that while the first click works and it goes to the tutorial step the second click which will close the tutorial doesn't click. For some reason time.sleep() works but I don't want to have to keep repeating that every step. Am I doing something wrong?
I have also tried find element by css as well.
Thank you.

The xpath to the second_click is not accurate in the sense that it does not send the click to correct element. try this out,
driver.fullscreen_window()
driver.get('https://www.ilcollege2career.com/#/')
first_click = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="tutorial-modal"]/div/div/div/div[3]/button[1]')))
first_click.click()
Option#1 -
second_click = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//button[#onclick='closeTutorial()']")))
second_click.click()
Option#2 -
second_click = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//div[#onclick='closeTutorial()']")))
second_click.click()

I found the solution to those who are looking for it.
invisible = WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.ID, 'tutorial-modal')))
if invisible:
There was something running behind that wouldn't close out so I was never able to close that and by doing this I was able to exit out.

Related

Can a scrollIntoView function save its position?

I am having a problem with JS scrollIntoView getting reset after a selenium webElement.click() was performed. When I have used it on a custom scrollbox, it performs action correctly and then scrolls back to top after hitting .click().
element = driver.findElement(By.xpath("//div[#id='iLegenda']/ul/li[4]/i"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
driver.findElement(By.xpath("//div[#id='iLegenda']/ul/li[4]/i")).click(); //this line resets the position of the affected scrollbar
An alternate solution would be force-clicking the element even though it's not displayed on screen, but I haven't found any info on that front yet...
That's not something you can control. It's something to do with whatever the click is doing. If it were me, I would put the .scrollIntoView() code into a function so that it's easier to call, e.g.
public void scrollIntoView(WebElement e)
{
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", e);
}
and call it like
element = driver.findElement(By.xpath("//div[#id='iLegenda']/ul/li[4]/i"));
scrollIntoView(element);
Also, Thread.Sleep() is a bad practice. (You can google to learn the reasons why). You shouldn't need a wait here anyway but in case you do, it should be something like
WebDriverWait wait = new WebDriverWait(driver, 2);
WebElement element = driver.findElement(locator);
wait.until(ExpectedConditions.elementToBeClickable(element)).click();
I use Java and Selenium all the time and I've never had to scroll before clicking an element. Have you tried something simple like
driver.findElement(By.xpath("//div[#id='iLegenda']/ul/li[4]/i")).click();
with no scroll and no wait? It should just work.
Also, if you are going to click the same element repeatedly, you should do something like
By locator = By.xpath("//div[#id='iLegenda']/ul/li[4]/i");
driver.findElement(locator).click();
// do something
driver.findElement(locator).click();
// ... and so on...

scrollTo javascript issue

I have a js function to scroll into a div.
Here is the function, It works fine, but i'm going to explain later what's wrong with this.
scrollAnchor= function(){
var element = document.getElementById('generalAnchor');
var position = element.getBoundingClientRect();
var x = position.left;
var y = position.top;
window.scrollTo(x,y);
}
Then an html (nevermind we just need the div generalAnchor)
<button onclick="scrollAnchor()">Click me</button>
<div id="generalAnchor">Test</div>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
This is en example of very long page, so the scroll works fine.
But here is the thing, if i remove all those br then, the scroll isn't working, because the page isn't long enough.
i could use a true url sometink like myPage#generalAnchor, but in fact i can't do that because i can't load a new page or i loose other informations.
Do you have an idea do deal with this ?
PS: I put the sample here: https://www.w3schools.com/code/tryit.asp?filename=FHHEHPNB4O1V
There is two case, the first button is working fine. But the second isn't working.
Thanks for your help!
Have you already thought about to use the default functionality of html anchor?
So if you replace the second button with
Click me
your code should work.
You can change the design if you want or wrap your button with the link.

IntersectionObserver blur element on unfocus

Recently I have been working on a project with an image grid for a friend of mine. The idea behind the website was that when the images in one row come into focus it pops out and the remaining go back into their original sizes.
After some research I found the IntersectionObserver API for JavaScript and the following example: http://codepen.io/pawelgrzybek/pen/YWqWXJ I modified the code to change the threshold to
let options = {
threshold: [1.0]
};
but those are just personal modifications, the issue I had was when I scroll down to the next element that comes into focus using the visible class but the previous elements still stay in focus. I wasn't sure about the best way to go about doing this.
I found examples and was able to notice the following code:
function updateStatus(visiblity) {
console.log(visiblity);
const status = document.querySelector('.status');
status.textContent = visiblity;
status.className = 'status status--' + visiblity;
}
but this is only for the top bar in the Simple Example I needed some help figuring this out, please and thank you everyone!

get the style of the marker I clicked on in mapbox

I'm trying to get the style of the icon which I clicked on, in order to get its translate values.
I'm doing this because I want to create a div that will have the same location on the map as the icon who got clicked on.
this is my website so far:
http://www.david-halfon.com/radio/index.html
Basically, each time someone presses the yellow circle, a div should appear around it.
this is the code for events that happen when an icon is being clicked:
locale.on('click', function(e) {
$("#music").attr("src", prop.Url);
$(".player").show();
play();
sendWithAjax(prop.Country, prop.City);
$("h1").html(prop.Station);
$("h2").html(prop.Country);
$("h3").html(prop.City);
//console.log($(this).css("transform"));
//console.log($(this).attr("style"));
console.log($(this));
setTimeout(function(){ $("h4").html(globalVar);}, 500);
$(".plusB").show();
$(".shareB").show();
map1.setView(locale.getLatLng(), 4);
playNow = prop.Station;
playNowCountry = prop.Country
playNowCity = prop.City;
});
The comments are what I have tried to do so far but it does'nt seems to work.
When I'm trying to consloe.log(this) I get 'undefined'.
Thank you!!
The reason why you don't get the expected $(this).css(...) informations is that this is not a regular DOM object, as it clearly appears using Firebug:
The <img> you want to get style from is contained in the _icon member of this object, so you can use e.g.:
console.log($(this._icon).css('transform'));
console.log($(this._icon).attr('style'));
// and so on
This way (tested using Firebug on your website), it works fine.
BTW, I couldn't figure out why the click event targets this object rather than the <img>...
BTW again, this website is a great idea: cool!

using onmouseover to change text hyperlink into an image hyperlink?

Please bear with me I am brand new to learning javascript (self taught)! I am usually one to find answers on my own from just web browsing but so far I haven't found any resources explaining how to accomplish the following:
So, basically all I want to do is change this (HTML):
SPEAKERS
to an image by using javascript.
The image is kept in the same folder as the html and the js.
Here is as far as I know to go with the javascript:
function showImage()
{
picture = new Image(100,100);
picture.src = "icon2.png";
document.getElementById("speakers").innerHTML = picture.src;
}
function goBack()
{
document.getElementById("speakers").innerHTML="SPEAKERS";
}
For clarity, all I would like to do is change the text ("SPEAKERS") to an image using 'onmouseover' while using the same hyperlink in the process.
It seems like a very simple problem but I don't know enough to determine if what I want to do is even possible. If it's not possible that's fine, I would just like to know either way ;P. Thanks ahead of time!
If you're ok with using jquery, you could use .html() and .hover()
http://jsfiddle.net/u8fsU/
Try something like this to get you started (not a complete nor tested solution):
var showImage = function(){
var picture = document.createElement("img");
picture.src = "icon2.png";
picture.href = "link.html";
var speakers = document.getElementById("speakers");
speakers.parentNode.replaceChild(speakers, picture);
}
Please see https://developer.mozilla.org/en-US/docs/Gecko_DOM_Reference for a good reference to some of the available DOM properties and methods.

Categories