简体   繁体   中英

How to click on an element generated by javascript using selenium

I'm trying to make a bot that creates accounts for me, but I can't interact with the element where I need to send my credentials.

All I know is that the element that I'm trying to interact with is generated in javascript, after clicking on another button. I found multiple answers but all were in others languages than Node.js.

I'm trying to send credentials on this element:

<input type="text" name="pseudo" id="pseudo" placeholder="Mon pseudo légendaire" style="margin-bottom: 10px;" maxlength="10">

I tried to use this:

driver.findElement(By.xpath('//*[@id="pseudo"]')).sendKeys('CREDITENTIALS')

Which returns me this error: Webdrivererror: element is not visible .

HTML element code looks like this :

<input type="text" name="pseudo" id="pseudo" placeholder="Mon pseudo légendaire" style="margin-bottom: 10px;" maxlength="10">

The problem is not that i have to wait until the element that im trying to interact with is displayed because it is already displayed, the problem is that i want to click on the second element that match with my findElement by xpath, because what im trying to click on is existing 2 times in the html code and only the second one is interactible.


Update (from the comments)

This element is within the following <div> tag:

<div id="modal_message_wrapper" class="block_scrollable_wrapper scrollbar-light yellow noise inscription">

You can construct an unique xpath clubbing up the id , name and placeholder attribute as follows:

driver.findElement(By.xpath("//input[@id='pseudo' and @name='pseudo' and @placeholder='Mon pseudo légendaire']")).sendKeys('CREDITENTIALS')

Update

As you mentioned that the desired element is within:

<div id="modal_message_wrapper" class="block_scrollable_wrapper scrollbar-light yellow noise inscription">

So you can use the following line of code:

driver.findElement(By.xpath("//div[@class='block_scrollable_wrapper scrollbar-light yellow noise inscription' and @id='modal_message_wrapper']//input[@id='pseudo' and @name='pseudo' and @placeholder='Mon pseudo légendaire']")).sendKeys('CREDITENTIALS')

Note : It's quite evident the element is within a Modal Dialog Box , so definitely you have to induce a waiter in the form of WebDriverWait before you attempt to send any character sequence to the <input> element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM