1

I have been researching for a way to change the element of a web page and have yet found (or understood) how to do it. In the above element I want to be able to change numerical value of to another number that is represented by a placeholder (i.e. nod for number of draws. Or do I have to do it manually every time I run the program?).

My OS is Ubunt 18.04 and using latest python, selenium and pycharm IDE.

These are just a couple of the websites I have visited and I have visited a lot.

https://www.youtube.com/watch?v=tdgAIDR9snc

http://allselenium.info/javascript-using-python-selenium-webdriver/

Set value of input instead of sendKeys() - selenium webdriver nodejs

WEBELEMENT:

<select name="ctl00$MainContent$ddlRange" id="ctl00_MainContent_ddlRange">
    <option selected="selected" value="1"> &nbsp;&nbsp;Single Draw</option>
    <option value="2"> &nbsp;&nbsp;2&nbsp;Consecutive</option>
    <option value="3"> &nbsp;&nbsp;3&nbsp;Consecutive</option>
    <option value="4"> &nbsp;&nbsp;4&nbsp;Consecutive</option>
    <option value="5"> &nbsp;&nbsp;5&nbsp;Consecutive</option>
    <option value="10">10&nbsp;Consecutive</option>
    <option value="20">20&nbsp;Consecutive</option>
</select>

And my basic code is:

from Selenium import WebDriver

browser = webdriver.Chrome('/usr/bin/chromedriver')
page=browser.get("whatever address of whatever webpage interested in")

elem=browser.find_element_by_css_selector("#ctl00_MainContent_ddlRange")
browser.execute_script("arguments[0].setAttribute('<option selected="selected" value="20">','20&nbsp;Consecutive</option>')", elem)

My goal is to change the value number in the first part of the option tag from:

<option selected="selected" value="1"> &nbsp;&nbsp;Single Draw</option>

to this:

<option selected="selected" value="44"> &nbsp;&nbsp;Single Draw</option>

In the execute_script I am always getting end of statement expected after the last ). I am also getting errors of ',' or ')' expected in the areas of "selected" and "20"

Also the css and xpath selectors to these are:

for <select name="ctl00$MainContent$ddlRange" id="ctl00_MainContent_ddlRange">
selector = #ctl00_MainContent_ddlRange
xpath    = //*[@id="ctl00_MainContent_ddlRange"]

for <option>
selector = #ctl00_MainContent_ddlRange > option:nth-child(1)   (note that nth-child() can be 1 - 7)
xpath    = //*[@id="ctl00_MainContent_ddlRange"]/option[1]     (note that option[] can also be 1 - 7)

I have the webpage saved to disc and I can go into the html and change that one number then save the file and when I click on it works. I just want to be able to automate it.

Any help is much appreciated

Idzireit

EDIT:

So far this is the closest I have come to actually inserting the value I want. The new code is as follows:

browser.execute_script("""arguments[0].setAttribute
('select', '<option selected="selected" value="nod"> &nbsp;&snbsp;draws </option>')""", get_draws)

Results in this:

<option value="20" select="<option selected=&quot;selected&quot; value=&quot;nod&quot;> &amp;nbsp;&amp;snbsp;draws </option>">20&nbsp;Consecutive</option>

The javascript I am trying to inject is getting injected in the middle of the element I am trying to modify (notice the option sequence repeats in the middle of the first bracket).

How can I correct this to make it look like this:

<option selected="selected" value="a number I am trying to change"> &nbsp;&nbsp;Draw<\option>

EDIT SOLVED:

Figured out how to change the value of an element. I had to change the search method of that element to this:

get_draws = browser.find_element_by_tag_name('option')

Then my next line was pretty simple:

browser.execute_script("""arguments[0].setAttribute('value', '100')""", get_draws)
idzireit
  • 98
  • 1
  • 7
  • Please [edit the question](/posts/55915856/edit) to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – undetected Selenium Apr 30 '19 at 08:14
  • You need to escape the " " characters around the 'selected' and '20'. – Mate Mrše Apr 30 '19 at 08:16
  • DebanjanB My apologies for rambling about ideas in other parts that I think have an affect on my code but simply put is how do it get the tag [option selected="selected" value="2"] to be [option selected="selected" value="34"] MateMrse not sure I understand what you mean by escaping the " " characters. – idzireit Apr 30 '19 at 08:39
  • @idzireit Update the question with the relevant HTML please – undetected Selenium Apr 30 '19 at 08:48
  • DebanjanB Hope this is edited correctly for you. – idzireit Apr 30 '19 at 15:38
  • @idzireit I wish moving forward you add the `@` before the username else the messages are not delivered directly to the inbox. I will take a relook in a couple of hours, once I reach home. – undetected Selenium Apr 30 '19 at 16:39

1 Answers1

1

For your second question, parametrizing the int value, try the following:

var = 100
js = f"arguments[0].setAttribute('value', '{var}')"
browser.execute_script(js, get_draws)

Just pass the value you want to the var variable.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • actually a moderator deleted the answer. Was up late last night so when I read your message I was wondering if I did and tried to undelete it but said moderator deleted it. Wonder why ? Oh yeah btw that worked great thanks. – idzireit May 08 '19 at 13:02